我编写了一个程序,其中包含两个使用mouseListeners的组件:
在Windows下一切正常。我今天尝试将程序移植到Mac,但MouseClicked事件永远不会被解雇。我把MouseClicked事件放在ResizeRectangle中,但它也不会被解雇。我把MouseReleased事件放在IconGrid中,然后被触发了。所以问题似乎与mouseClicked事件有关。
我读到另一篇文章说,在Mac上,即使鼠标按下和鼠标释放之间的小像素变化也会导致MouseClicked没有被触发。但即使我在空中点击鼠标(因此没有机会在按下和释放之间移动鼠标),mouseClicked事件也不会被触发。
其他人有这个问题吗?这是Mac上的错误吗?
答案 0 :(得分:0)
我在OS X上用JDK1.7尝试了以下代码,我可以点击图标并触发监听器。请随意修改此代码以符合您的情况,以便我们重现问题并将其包含在您的问题中。
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
public class MouseClickedIcon {
public static void showUI() throws IOException {
JFrame testFrame = new JFrame( "TestFrame" );
String imageSource = "http://www.mynewitguys.com/wp-content/uploads/2011/04/java1.png";
ImageIcon icon = new ImageIcon( ImageIO.read( new URL( imageSource ) ) );
JLabel label = new JLabel( icon );
label.addMouseListener( new MouseAdapter() {
@Override
public void mouseClicked( MouseEvent e ) {
System.out.println( "MouseClickedIcon.mouseClicked" );
}
} );
testFrame.add( label, BorderLayout.CENTER );
testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
testFrame.pack();
testFrame.setVisible( true );
}
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
@Override
public void run() {
try {
showUI();
} catch ( IOException e ) {
}
}
} );
}
}
答案 1 :(得分:0)
得到错误的来源。我在虚拟机(VMWARE)中运行OSX并检查了鼠标兼容性选项,这导致了MouseClicked事件的问题。抱歉,麻烦。