Java:JMenuItem在单击时抛出异常:IllegalComponentStateException

时间:2013-12-19 11:58:14

标签: java exception jmenu jmenuitem jmenubar

我正在尝试在我的程序中使用JMenuBar,但每次单击JMenuItem时都会抛出异常。

我已经制作了一个非常简单的代码片段来解释我目前如何使用JMenuBar,JMenu和JMenuItem:

import javax.swing.*;

public class JMenuTest extends JFrame {
    JMenuTest() {
        super("Tester");

        JMenuBar tb = new JMenuBar();
        JMenu a = new JMenu("File");
        JMenuItem z = new JMenuItem("Öppna osv");   

        tb.add(a);
        a.add(z);

        setJMenuBar(tb);

        setSize(300, 300);
        setVisible(true);
    }   

    public static void main(String[] args) {
        new JMenuTest();
    }
}

这是堆栈跟踪:

java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
    at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:1995)
    at java.awt.Component.getLocationOnScreen(Component.java:1969)
    at apple.awt.CAccessibility$22.call(CAccessibility.java:370)
    at apple.awt.CAccessibility$22.call(CAccessibility.java:368)
    at apple.awt.CToolkit$CallableWrapper.run(CToolkit.java:1125)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
    at apple.awt.CToolkit$CPeerEvent.dispatch(CToolkit.java:1215)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

非常感谢您的回复!

0 个答案:

没有答案