gpt4 book ai didi

java - 未触发 MAC OS X Java Swing Mouse Released 事件

转载 作者:搜寻专家 更新时间:2023-11-01 03:38:50 24 4
gpt4 key购买 nike

我遇到了 Java Swing 鼠标释放事件的问题,该事件似乎只出现在 mac os java 实现上(在 Windows Java 上没有这样的问题)。

我的配置:操作系统 X 10.9Java版本: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 的“鼠标释放”事件在所有情况下都可以完美触发,但以下情况除外:

  1. 点击空白区域(黄色方 block )
  2. 将鼠标拖出应用程序的主框架
  3. 将鼠标在主框架内返回到文本区域(而不是进入源空白区域)
  4. 松开鼠标按钮。结果-> 未触发鼠标释放。 (OS X Java 特定问题)

提前感谢您的帮助。

编辑:我在主框架的底部添加了一个 TextField。现在,如果重复上述所有案例步骤,但恰好在 TextField 上释放鼠标,则会触发“mouse_released”事件,但仍不会在 TextArea 上触发。

最佳答案

在 Mac OS X 上使用 Java 6 时观察到的行为也很普遍。请注意 getComponent() “返回事件的发起者。”通常,您的程序不应依赖于异常的、特定于平台的结果。由于未记录 Windows 行为,您的实际目标可能会提供更可靠的方法。

更正:这是 Mac OS X 上 1.7.0_45 中的回归,在 JDK-8013475 中进行了检查.

关于java - 未触发 MAC OS X Java Swing Mouse Released 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20497629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com