MouseAdapter class

I have problem when want to get mouse click count using mouse adapter class and having an error like this :
Exception in thread "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) 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(Unknown Source) at java.awt.Window.processEvent(Unknown Source) 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) at java.awt.E ventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at 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.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) at java.awt.EventDispatchThread.run(Unknown Source )`

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();
    }

}

The exception message is a good indicator of the problem - you need to use a format specifier

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

Read the javadoc


This

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

should be

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

Note de %d


您缺少格式说明符%d

details = String.format("you clicked %d",event.getClickCount();
链接地址: http://www.djcxy.com/p/73432.html

上一篇: 自动将org.mysql更改为org.sqlite

下一篇: MouseAdapter类