MouseAdapter类

时间:2013-06-20 15:21:47

标签: java awt mouselistener

当想要使用鼠标适配器类获得鼠标点击计数并出现如下错误时,我遇到了问题:
线程“AWT-EventQueue-0”中的异常java.util.FormatFlagsConversionMismatchException:Conversion = c,Flags =
    at 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)     在java.util.Formatter.parse(未知来源)     at java.util.Formatter.format(Unknown Source)     at java.util.Formatter.format(Unknown Source)     在java.lang.String.format(未知来源)     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(Unknown Source)     在java.awt.Window.processEvent(未知来源)     at java.awt.Component.dispatchEventImpl(Unknown Source)     at 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)     在java.awt.EventQueue.access $ 200(未知来源)     在java.awt.EventQueue $ 3.run(未知来源)     在java.awt.EventQueue $ 3.run(未知来源)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)     在java.awt.EventQueue $ 4.run(未知来源)     在java.awt.EventQueue $ 4.run(未知来源)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)     at 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(Unknown Source)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)     在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();
    }

}

3 个答案:

答案 0 :(得分:1)

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

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

阅读javadoc

答案 1 :(得分:0)

这个

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

应该是

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

注意de%d

答案 2 :(得分:0)

您缺少格式说明符%d

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