首页 > 解决方案 > 第二次调用类程序中断,Java

问题描述

好吧,请耐心等待我,因为我真的很新。为了保持简单和简短,我有一个具有 jButton(在登录框架上)的类,该类具有应该从另一个类打开框架的操作,如下所示:

if (user.contains("admin") && pass.contains("admin"))
            {
                Admin adminProz = new Admin();
                adminProz.setVisible(true);
                frame.setVisible(false);
            }

暂时它工作得很好,但后来我回到“登录”页面进行第二次登录尝试,编译器中断......以下是我在控制台中得到的文本:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is displayable.
at java.awt.Frame.setUndecorated(Unknown Source)
at prozori.Admin.<init>(Admin.java:50)
at prozori.LoginWin.<init>(LoginWin.java:53)
at prikaz.Logout$1.actionPerformed(Logout.java:36)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

标签: javaswing

解决方案


在 Frame 可见后,不应调用您尝试调用的方法。

这在java.awt.Frame的文档中有所介绍

摘自 javadoc:

This method can only be called while the frame is not displayable.

Throws:
  IllegalComponentStateException - if the frame is displayable

setUndecorated因此,只需在显示 Frame 之前将其移动到您调用的位置。


推荐阅读