0

See the sample code below. It simply creates a button and adds it to a window. But when *menu_item3* is selected, the ActionListener doesn't receive the event. This error only occurs on Java 7 for Mac. If I run this same code in Windows, it works fine. When I run this same code on Java 6 for Mac, it works fine. If I use a JFrame instead of JWindow, it works fine. I do not want to use a JFrame because I do not want to display the window title bar and border.

Any ideas?

public class SandBox {
 public static JFrame frame = new JFrame();
 public static JPopupMenu menu = new JPopupMenu();
 public static JLabel button = new JLabel();

 public static void main(String[] args) {
     JFrame window = new JFrame();
     JPanel panel = new JPanel();

     JMenuItem menu_item1 = new JMenuItem("Item1");
     JMenuItem menu_item2 = new JMenuItem("Item2");
     JMenuItem menu_item3 = new JMenuItem("Item3");

     menu.add(menu_item1);
     menu.add(menu_item2);
     menu.add(menu_item3);
     menu.setEnabled(true);

     button.setText("Button");
     button.setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, new Color(255,0,0)));
     button.setSize(100, 24);
     button.add(menu);
     button.setVisible(true);
     button.setEnabled(true);

     panel.add(button);
     panel.setVisible(true);

     window.add(panel);
     window.setVisible(true);
     window.setLocation(100, 100);
     window.setAlwaysOnTop(true);
     window.setFocusable(true);
     window.setFocusableWindowState(true);
     window.pack();

     frame.setVisible(false);

     button.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent e) {
             menu.show(button, 0, 0);
         }
     });

     menu_item3.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");
         }
     });
 }
}
4

1 回答 1

0

我已经向 Oracle 提交了一个错误。还在等待他们的回应是否会正式提交该错误。当我听到一些东西时,我会更新这个答案。

与此同时,我确实找到了一个可行的解决方法。我使用 JFrame 而不是 JWindow。我不知道您可以使用setUndecorated()方法删除 JFrame 的窗口标题和边框。另请注意,此方法只能在框架不可显示时调用。

于 2012-10-31T16:38:30.890 回答