-1

当我想使用鼠标适配器类获取鼠标点击计数并且出现这样的错误时遇到问题:
线程“AWT-EventQueue-0”中的异常 java.util.FormatFlagsConversionMismatchException: Conversion = c, Flags =
在 java.util.Formatter$FormatSpecifier.failMismatch(Unknown Source) at java.util.Formatter$FormatSpecifier.checkBadFlags(Unknown Source) at java.util.Formatter$FormatSpecifier.checkCharacter(Unknown Source) at java.util.Formatter$FormatSpecifier .(Unknown Source) at java.util.Formatter.parse(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.lang.String.format( Unknown Source) at practice2.window12$Mouseclass.mouseClicked(window12.java:20) at java.awt.Component.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent (未知源)在 java.awt.Window.processEvent(未知源)在 java.awt.Component.dispatchEventImpl(未知源)在 java.awt.Container。dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200 (未知源)在 java.awt.EventQueue$3.run(未知源)在 java.awt.EventQueue$3.run(未知源)在 java.security.AccessController.doPrivileged(Native Method) 在 java.security.ProtectionDomain$1。 doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController java.security.ProtectionDomain 的 .doPrivileged(Native Method) $1.doIntersectionPrivilege(Unknown Source) 在 java.awt.EventQueue。dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(未知来源)在 java.awt.EventDispatchThread.pumpEvents(未知来源)在 java.awt.EventDispatchThread.run(未知来源)`运行(未知来源)`运行(未知来源)`

package practice2;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class window12 extends JFrame{
    private String details;
    private JLabel statusbar;

    public window12(){
        super("this will be title");
        setVisible(true);
        setSize(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        statusbar=new JLabel("this will be defalt");
        add(statusbar,BorderLayout.SOUTH);
        addMouseListener(new Mouseclass());
    }
    private class Mouseclass extends MouseAdapter{
        public void mouseClicked(MouseEvent event){
            details=String.format("you clicked %      `enter code here`clicks",event.getClickCount());
            if(event.isMetaDown()){
                details+=" with right button";
            }
            else if(event.isAltDown()){
                details+=" with center button";
            }
            else{
                details+=" with left button";
            }
            statusbar.setText(details);
        }
    }
    public static void main(String[] args){
        new window12();
    }

}
4

3 回答 3

1

异常消息是问题的一个很好的指标 - 您需要使用格式说明符

details = String.format("you clicked %d", event.getClickCount());

阅读javadoc

于 2013-06-20T15:24:07.007 回答
0

您缺少格式说明符%d

details = String.format("you clicked %d",event.getClickCount();
于 2013-06-20T15:26:01.487 回答
0

这个

details=String.format("you clicked %      `enter code here`clicks",event.getClickCount());

应该

details=String.format("you clicked %d      `enter code here`clicks",event.getClickCount());

注释 %d

于 2013-06-20T15:25:02.377 回答