Java Swing中的IllegalComponentStateException

Java Swing中的IllegalComponentStateException,java,swing,java-7,Java,Swing,Java 7,在执行基于java的swing程序时,我遇到了一个奇怪的错误,我只在我的计算机上遇到过这个错误(我在另外两台计算机上试过,结果都很好) 问题是当我点击下拉菜单选择一个选项时出现错误。错误是: java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location at java.awt.Component.getLocationOnScre

在执行基于java的swing程序时,我遇到了一个奇怪的错误,我只在我的计算机上遇到过这个错误(我在另外两台计算机上试过,结果都很好)

问题是当我点击下拉菜单选择一个选项时出现错误。错误是:

    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:527)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:241)
    at sun.lwawt.macosx.LWCToolkit$CPeerEvent.dispatch(LWCToolkit.java:684)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:155)
    at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:182)
    at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:221)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:219)
    at java.awt.Dialog.show(Dialog.java:1077)
    at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:583)
    at ModalDialogsTest.simpleStringChoiceDialog(ModalDialogsTest.java:109)
    at ModalDialogsTest.actionPerformed(ModalDialogsTest.java:70)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    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:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
    choice 2
正如你所看到的,一个错误发生了,但我仍然可以得到函数的结果,所以我不知道什么是错误的。。。有线索吗?我正在MacOS X Mountain Lion上使用Java7。 好的,下面是你问我的结果:
System.out.println(System.getProperty(“java.vm.name”);给我:

Java HotSpot(TM) 64-Bit Server VM.
System.getProperties().list(System.out);给我:

-- listing properties --
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=/Library/Java/JavaVirtualMachines/jdk...
java.vm.version=23.3-b01
user.country.format=IE
gopherProxySet=false
java.vm.vendor=Oracle Corporation
java.vendor.url=http://java.oracle.com/
path.separator=:
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg=sun.io
user.country=FR
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=unknown
java.vm.specification.name=Java Virtual Machine Specification
user.dir=/Users/hugo/Documents/workspace/dialogs
java.runtime.version=1.7.0_07-b10
java.awt.graphicsenv=sun.awt.CGraphicsEnvironment
java.endorsed.dirs=/Library/Java/JavaVirtualMachines/jdk...
os.arch=x86_64
java.io.tmpdir=/var/folders/2n/q2bb2df90qqb_x38djlwx...
line.separator=

java.vm.specification.vendor=Oracle Corporation
os.name=Mac OS X
sun.jnu.encoding=US-ASCII
java.library.path=/Users/hugo/Library/Java/Extensions:/...
java.specification.name=Java Platform API Specification
java.class.version=51.0
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
os.version=10.8
http.nonProxyHosts=local|*.local|169.254/16|*.169.254/16
user.home=/Users/hugo
user.timezone=
java.awt.printerjob=sun.lwawt.macosx.CPrinterJob
file.encoding=US-ASCII
java.specification.version=1.7
user.name=hugo
java.class.path=/Users/hugo/Documents/workspace/dialo...
java.vm.specification.version=1.7
sun.arch.data.model=64
java.home=/Library/Java/JavaVirtualMachines/jdk...
sun.java.command=ModalDialogsTest
java.specification.vendor=Oracle Corporation
user.language=fr
user.language.format=en
awt.toolkit=sun.lwawt.macosx.LWCToolkit
java.vm.info=mixed mode
java.version=1.7.0_07
java.ext.dirs=/Users/hugo/Library/Java/Extensions:/...
sun.boot.class.path=/Library/Java/JavaVirtualMachines/jdk...
java.vendor=Oracle Corporation
file.separator=/
java.vendor.url.bug=http://bugreport.sun.com/bugreport/
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeBig
sun.font.fontmanager=sun.font.CFontManager
socksNonProxyHosts=local|*.local|169.254/16|*.169.254/16
ftp.nonProxyHosts=local|*.local|169.254/16|*.169.254/16
sun.cpu.isalist=

编辑:我忘了提到我在另一台mac电脑上试过,和我的完全一样(除了我的有更多内存,但这并不重要)和在windows 7电脑上试过。

重写,因为满是鳗鱼的气垫船说:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            ModalDialogsTest newWindow = new ModalDialogsTest();
            newWindow.setVisible(true);
        }
    });

}
该异常不应再出现

编辑:

使用OracleJava7编译并运行,没有问题。看起来OpenJDK for Mac有一些缺点。如果我没有看到这一点,我会确信你没有发布整个stacktrace。请再次确认您已发布整个stacktrace。如果您已发布整个stacktrace,则:

  • 您使用的是OpenJDK还是

  • 另外两台电脑也是Mac还是

  • 要检查您正在使用的JVM实现,请在代码中插入以下行:

    System.out.println(System.getProperty("java.vm.name"));
    
    并发布输出。如果输出为空,则将此行放入:

    System.getProperties().list(System.out);
    

    并发布输出。

    只是一个想法。将
    面板
    变量设为全局变量,然后将
    showInputDialog
    的第一个参数从
    更改为
    面板

                String s = (String)JOptionPane.showInputDialog(
                                    panel, null, null,
                                    JOptionPane.PLAIN_MESSAGE, null,
                                    possibilities, possibilities[0]);
    
    编辑:

    我想这些线

    at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:583)
    at ModalDialogsTest.simpleStringChoiceDialog(ModalDialogsTest.java:109)
    
    建议问题出在
    showInputDialog
    parentComponent
    参数上。更改代码并创建
    JFrame
    ,而不是扩展它。另见和

    编辑2:

    我稍微修改了你的代码,运行它,如果异常仍然存在,那么我不知道还有什么问题

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class ModalDialogsTest implements ActionListener{
    
        private JButton choice;
        private JPanel panel = new JPanel();
        private JFrame frame = new JFrame();
    
        ModalDialogsTest() {
            frame.getContentPane().add(panel);
            panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
            choice = new JButton("My button");
            panel.add(choice);
            choice.addActionListener(this);        
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400,300);
            frame.setVisible(true);
        }
    
        public void actionPerformed(ActionEvent event) {
            simpleStringChoiceDialog();
        }
    
        private void simpleStringChoiceDialog() {
            Object[] possibilities = {"choice 1", "choice 2", "choice 3"};
            String s = (String)JOptionPane.showInputDialog(
                        frame.getContentPane(), null, null,
                        // panel, null, null,                   // <-- or this
                        JOptionPane.PLAIN_MESSAGE, null,
                        possibilities, possibilities[0]);
                        System.out.println(s);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new ModalDialogsTest();
                }
            });
        }
    }
    
    导入java.awt.event.ActionEvent;
    导入java.awt.event.ActionListener;
    导入javax.swing.BoxLayout;
    导入javax.swing.JButton;
    导入javax.swing.JFrame;
    导入javax.swing.JOptionPane;
    导入javax.swing.JPanel;
    导入javax.swing.SwingUtilities;
    公共类ModalDialogsTest实现ActionListener{
    私人按钮选择;
    private JPanel panel=new JPanel();
    私有JFrame=新JFrame();
    ModalDialogsTest(){
    frame.getContentPane().add(面板);
    panel.setLayout(新的BoxLayout(panel,BoxLayout.Y_轴));
    选择=新的JButton(“我的按钮”);
    面板。添加(选择);
    choice.addActionListener(this);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    框架。设置尺寸(400300);
    frame.setVisible(true);
    }
    已执行的公共无效操作(操作事件){
    simpleStringChoiceDialog();
    }
    private void simpleStringChoiceDialog(){
    对象[]可能性={“选择1”、“选择2”、“选择3”};
    字符串s=(字符串)JOptionPane.showInputDialog(
    frame.getContentPane(),null,null,
    //panel,null,null,//您是否正在使用(其他方面非常优秀的)窗口管理增强器

    我总是在某些Java GUI中遇到这种错误,即使是像Matlab这样的商业软件

    除了凌乱的控制台,它似乎从未真正干扰过任何功能


    不幸的是,我没有解决方案。但也许知道这可能是原因是有帮助的。

    我在Mac Yosemite上使用Java 7时也遇到了同样的问题。在我的例子中,这是由第三方工具“Cinch”引起的我用来在桌面上排列窗口的。停用该工具会使异常消失。

    请理解,我们中的许多人都有防火墙,阻止我们单击链接。不管怎样,如果你要求免费咨询,你应该让人们尽可能容易地帮你,包括在本网站上发布相关代码我已经在Java6 LinuxMint13上运行了提供的代码,没有问题,我将安装Java7并提供反馈shortly@linski如果你的答案是关于从EDT打swing电话:不,他不是。这是问题最可能的原因。@linski:不,他不是。他需要创建一个Runnable并创建他的在run方法中初始化,然后将其传递给
    SwingUtilities.invokeLater(…)
    谢谢。我刚刚尝试过,但它没有改变任何东西…:/还有其他想法吗?我刚下载了Java7,BRBIn如果它可能会改变什么,我正在Eclipse Juno中运行我的代码。我用正确的答案回答了您,因为这样更容易让它可读。请参阅下面(:JFrame是组件的子类,所以这不重要,但请尝试确定。@koleror尝试使用
    null
    作为参数如何创建Iframe?抱歉,我觉得有点生疏:/这正是它(使用Flexiglas).谢谢!是的,它确实对我也起了作用!在经历了这么多年的恼人问题之后,我很高兴它终于被修复了!非常感谢!这真的很奇怪,因为Cinch在后台做了什么会让JComboBox“忘记”呢它正在显示。我最好的猜测可能是它在内存中做了一些有趣的事情,它可能会清除一点,表明组合框没有显示,而实际上是。但这似乎不太可能,所以我不确定。谢谢!这解决了我在Macbook Pro的matlab中遇到的问题。:)
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class ModalDialogsTest implements ActionListener{
    
        private JButton choice;
        private JPanel panel = new JPanel();
        private JFrame frame = new JFrame();
    
        ModalDialogsTest() {
            frame.getContentPane().add(panel);
            panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
            choice = new JButton("My button");
            panel.add(choice);
            choice.addActionListener(this);        
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400,300);
            frame.setVisible(true);
        }
    
        public void actionPerformed(ActionEvent event) {
            simpleStringChoiceDialog();
        }
    
        private void simpleStringChoiceDialog() {
            Object[] possibilities = {"choice 1", "choice 2", "choice 3"};
            String s = (String)JOptionPane.showInputDialog(
                        frame.getContentPane(), null, null,
                        // panel, null, null,                   // <-- or this
                        JOptionPane.PLAIN_MESSAGE, null,
                        possibilities, possibilities[0]);
                        System.out.println(s);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new ModalDialogsTest();
                }
            });
        }
    }