[英]MAC OS X Java Swing Mouse Released event is not fired
I've faced a problem with Java swing mouse released event which appears seems on mac os java implementation only (there is no such problem on Windows Java). 我遇到了Java swing鼠标发布事件的问题,它似乎只出现在mac os java实现上(在Windows Java上没有这样的问题)。
My configuration: OS X 10.9 Java version: 1.7.0_45, vendor: Oracle Corporation 我的配置:OS X 10.9 Java版本:1.7.0_45,供应商:Oracle Corporation
To repeat this problem run this oracle tutorial demo class 要重复此问题,请运行此oracle教程演示类
public class MouseEventDemo extends JPanel
implements MouseListener {
BlankArea blankArea;
JTextArea textArea;
static final String NEWLINE = System.getProperty("line.separator");
public static void main(String[] args) {
/* Use an appropriate Look and Feel */
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("MouseEventDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new MouseEventDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public MouseEventDemo() {
super(new GridLayout(0,1));
blankArea = new BlankArea(Color.YELLOW);
add(blankArea);
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(200, 75));
add(scrollPane);
TextField tf=new TextField(10);
add(tf);
//Register for mouse events on blankArea and the panel.
blankArea.addMouseListener(this);
addMouseListener(this);
setPreferredSize(new Dimension(450, 450));
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
void eventOutput(String eventDescription, MouseEvent e) {
textArea.append(eventDescription + " detected on "
+ e.getComponent().getClass().getName()
+ "." + NEWLINE);
textArea.setCaretPosition(textArea.getDocument().getLength());
}
public void mousePressed(MouseEvent e) {
eventOutput("Mouse pressed (# of clicks: "
+ e.getClickCount() + ")", e);
}
public void mouseReleased(MouseEvent e) {
eventOutput("Mouse released (# of clicks: "
+ e.getClickCount() + ")", e);
}
public void mouseEntered(MouseEvent e) {
eventOutput("Mouse entered", e);
}
public void mouseExited(MouseEvent e) {
eventOutput("Mouse exited", e);
}
public void mouseClicked(MouseEvent e) {
eventOutput("Mouse clicked (# of clicks: "
+ e.getClickCount() + ")", e);
}
public class BlankArea extends JLabel {
Dimension minSize = new Dimension(100, 50);
public BlankArea(Color color) {
setBackground(color);
setOpaque(true);
setBorder(BorderFactory.createLineBorder(Color.black));
}
public Dimension getMinimumSize() {
return minSize;
}
public Dimension getPreferredSize() {
return minSize;
}
}
}
"Mouse Release" event for BlankArea is fired perfectly in all cases except the following: 除以下情况外,所有情况下都会完全触发BlankArea的“Mouse Release”事件:
Thanks for your help in advance. 感谢您的帮助。
EDITED: I've added a TextField at the bottom of main frame. 编辑:我在主框架的底部添加了一个TextField。 Now, if repeat all case steps described above, but release mouse exactly on TextField the "mouse_released" event is fired, but still not on TextArea.
现在,如果重复上述所有案例步骤,但是在TextField上完全释放鼠标,则会触发“mouse_released”事件,但仍然不在TextArea上。
The observed behavior also prevails using Java 6 on Mac OS X. Note that getComponent()
"Returns the originator of the event." 在Mac OS X上使用Java 6也可以看到观察到的行为。请注意,
getComponent()
“返回事件的发起者”。 In general, your program should not rely on anomalous, platform-specific results. 通常,您的程序不应该依赖异常的,特定于平台的结果。 As the Windows behavior is not documented, your actual goal may inform a more reliable approach.
由于未记录Windows行为,因此您的实际目标可能会提供更可靠的方法。
Correction: This is a regression in 1.7.0_45
on Mac OS X, examined in JDK-8013475
. 更正:这是在Mac OS X上
1.7.0_45
的回归,在JDK-8013475
进行了检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.