gpt4 book ai didi

java - 错误: IllegalComponentStateException when pressing JMenuItem in java swing (OSX)

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:18 24 4
gpt4 key购买 nike

在一个项目期间,我偶然发现了一条令人困惑的错误消息,该消息在单击任何项​​目的 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

最佳答案

您的程序的以下变体适用于带有 Java 8 的 Mac OS X 10.9。请注意,Swing GUI 对象应该event dispatch thread 上构建和操作。 .

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);
}
});
}
}

关于java - 错误: IllegalComponentStateException when pressing JMenuItem in java swing (OSX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27016400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com