我在Java Swing鼠标释放事件上遇到了问题,这个问题似乎只出现在Mac OS的Java实现中(在Windows Java上没有这个问题)。
我的配置: OS X 10.9 Java版本:1.7.0_45,供应商:Oracle Corporation
要重复这个问题,请运行此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;
}
}
}
除以下情况外,“BlankArea”的“Mouse Release”事件在所有情况下都能正常触发:
- 点击空白区域(黄色方框)
- 将鼠标拖动到应用程序主窗体之外
- 将鼠标返回到文本区域内(而不是源空白区域)
- 松开鼠标按钮。 结果->鼠标释放没有被触发。(OS X Java 特定问题)
编辑: 我已经在主窗体底部添加了一个文本框。 现在,如果重复上述所有步骤,但确切地在文本字段上释放鼠标,则“mouse_released”事件将被触发,但仍无法在TextArea上触发。
1.7.0_45
上可以重现。 - trashgod