MouseAdapter类
当我想使用鼠标适配器类获得鼠标点击计数并出现如下错误时,我遇到了问题:
线程“AWT-EventQueue-0”中的异常java.util.FormatFlagsConversionMismatchException:Conversion = c,Flags =
java.util.Formatter上的$ FormatSpecifier.failMismatch(Unknown Source)java.util.Formatter上的$ FormatSpecifier.checkBadFlags(Unknown Source)$ java.util.Formatter上的$ FormatSpecifier.checkCharacter(Unknown Source)$ FormatSpecifier (Unknown Source)at java.util.Formatter.format(Unknown Source)at java.util.Formatter.format(Unknown Source)at java.util.Formatter.format(Unknown Source)at java.util.Formatter.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源)在java.awt.Component.dispatchEvent(未知源)在java.awt.EventQueue.dispatchEventImpl(未知源)在java.awt.E 在java.awt.EventQueue上的$ java.util.EccessQueue $ 3.run(Unknown Source)java.security.AccessController.doPrivileged(Native Method)上的java.security $ 3.run(Unknown Source) .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 java.security.ProtectionDomain上的.security.AccessController.doPrivileged(Native方法)位于java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)上的java.awt.EventQueue.dispatchEvent(Unknown Source)上的$ 1.doIntersectionPrivilege(Unknown Source)。在java.awt中的java.awt.EventDispatchThread.pumpEvents(Unknown Source)上的java.awt.EventDispatchThread.pumpEvents(Unknown Source)上的java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)上的awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 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();
}
}
异常消息是问题的良好指示器 - 您需要使用格式说明符
details = String.format("you clicked %d", event.getClickCount());
阅读javadoc
这个
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
您缺少格式说明符%d
:
details = String.format("you clicked %d",event.getClickCount();
链接地址: http://www.djcxy.com/p/73431.html
上一篇: MouseAdapter class