错误:在java swing(OSX)中按JMenuItem时出现IllegalComponentStateException

时间:2014-11-19 12:05:22

标签: java macos swing

在项目期间,我偶然发现了一个混乱的错误消息,该消息在单击任何项​​目的JMenuItems时出现。该程序确实继续运行,但错误日志变得一团糟。我还发现,当从Windows运行时(通过macbook上的VM),同一程序不会给出任何错误消息。我尝试构建一个只显示JMenuItem的极其简化的版本,但仍然出现错误消息。

这是我写的愚蠢的JMenuItem测试程序:

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class TestBug {
public static JMenuItem menuItem; 

public static void main(String args[]){
    JFrame frame = new JFrame("Java Praktikum");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocation(100,100);
    frame.setSize(800, 600);

    JMenuBar menuBar = new JMenuBar();
    JMenu testMenu = new JMenu();
    menuItem = new JMenuItem("testMenuItem");

    testMenu.add(menuItem);
    menuBar.add(testMenu);
    frame.setJMenuBar(menuBar);

    frame.pack();
    frame.setVisible(true); 
}}

这是每次按下JMenuItem时显示的错误消息:

java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:2044)
at java.awt.Component.getLocationOnScreen(Component.java:2018)
at sun.lwawt.macosx.CAccessibility$22.call(CAccessibility.java:390)
at sun.lwawt.macosx.CAccessibility$22.call(CAccessibility.java:388)
at sun.lwawt.macosx.LWCToolkit$CallableWrapper.run(LWCToolkit.java:504)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at sun.lwawt.macosx.LWCToolkit$4.dispatch(LWCToolkit.java:529)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

在那里有一些关于macosx的内容,但谷歌并没有帮助我。

向前谢谢! < 3

1 个答案:

答案 0 :(得分:1)

以下版本的程序适用于Mac OS X 10.9 with Java 8.请注意,应在event dispatch thread上构建和操作Swing GUI对象

image

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class TestBug {

public static JMenuItem menuItem; 

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Java Praktikum");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JMenuBar menuBar = new JMenuBar();
                JMenu testMenu = new JMenu("testMenu");
                menuItem = new JMenuItem("menuItem");
                testMenu.add(menuItem);
                menuBar.add(testMenu);
                frame.setJMenuBar(menuBar);
                frame.pack();
                frame.setVisible(true); 
            }
        });
    }
}