MAC OS X Java Swing鼠标释放事件未被触发。

4

我在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”事件在所有情况下都能正常触发:
  1. 点击空白区域(黄色方框)
  2. 将鼠标拖动到应用程序主窗体之外
  3. 将鼠标返回到文本区域内(而不是源空白区域)
  4. 松开鼠标按钮。 结果->鼠标释放没有被触发。(OS X Java 特定问题)
感谢您的帮助。
编辑: 我已经在主窗体底部添加了一个文本框。 现在,如果重复上述所有步骤,但确切地在文本字段上释放鼠标,则“mouse_released”事件将被触发,但仍无法在TextArea上触发。
1个回答

1
观察到的行为在 Mac OS X 上使用 Java 6 也是如此。请注意,getComponent()“返回事件的发起者”。一般来说,您的程序不应依赖异常的、特定于平台的结果。由于 Windows 的行为没有记录,您的实际目标可能会提示更可靠的方法。
更正:这是在 Mac OS X 上 1.7.0_45 中的一个回归,在 JDK-8013475 中进行了检查。

1
感谢您的建议。实际上,这个问题已经在JDK Bug System中被指定,并且已被标记为在JDK-8和JDK7u40中均无法重现。不幸的是,它仍然存在于JDK7u45中。 - user3065320
啊,我误用了帧外步骤,在1.7.0_45上可以重现。 - trashgod

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接