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

时间:2013-12-10 14:55:10

标签: java macos swing mouseevent

我遇到了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的“鼠标释放”事件:

  1. 按空白区域(黄色方块)
  2. 将鼠标拖到应用程序主框架之外
  3. 将主框架内的鼠标返回文本区域(不是源空白区域)
  4. 释放鼠标按钮。 Result->鼠标释放未被触发。 (OS X Java特定问题)
  5. 提前感谢您的帮助。

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

1 个答案:

答案 0 :(得分:1)

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

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