JFrame构造函数在生成的代码中失败

时间:2016-03-15 23:37:51

标签: java swing jframe

为我的窗口启动线程的简化代码:

public static LiDrThread lidrThread;
public void onKeyInput(InputEvent.KeyInputEvent event){
   if( lidrThread== null || !lidrThread.isAlive())
   {
      lidrThread= new LiDrThread();
      lidrThread.start();
   }
}

我的LiDrThread类中的Overriden run方法,它扩展了Thread

private LightDrafterWindow window;
@Override
public void run() {
    try {
        LogHelper.info("Initializing window");
        window = new LightDrafterWindow();
        LogHelper.info("Setting window Visible");
        window.setVisible(true); // altered since original post
    } catch (Exception e) {
        LogHelper.warn("Window failed to open");
        e.printStackTrace();
    }
}

这是我的构造函数的简化代码,我的窗口扩展了JFrame:

public JPanel contentPane;
public LightDrafterWindow() {
    LogHelper.info("Accessing PlayerName");
    LogHelper.info("Accessing Player Capabilities");

    LogHelper.info("Setting up basics of Window");
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // line 48
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    setContentPane(contentPane);
    contentPane.setLayout(null);

    //PlayerInfo section -----------------------------------------------------------------------------------------------
    LogHelper.info("Initializing player info panel");
    contentPane.setVisible(true);
}

在最后一个LogHelper之后还有更多,但这是我的控制台中显示的内容:

[18:12:46] [Thread-15/INFO] [lidr]: Initializing window
[18:12:46] [Thread-15/INFO] [lidr]: Accessing PlayerName
[18:12:46] [Thread-15/INFO] [lidr]: Accessing Player Capabilities
[18:12:46] [Thread-15/INFO] [lidr]: Setting up basics of Window
[18:12:46] [Thread-15/WARN] [lidr]: Window failed to open
[18:51:14] [Thread-15/INFO] [STDERR]: [main.java.lidr.thread.LiDrThread:run:23]: java.lang.ArrayIndexOutOfBoundsException: 5
[18:51:14] [Thread-15/INFO] [STDERR]: [main.java.lidr.thread.LiDrThread:run:23]:    at net.minecraftforge.fml.relauncher.FMLSecurityManager.checkPermission(FMLSecurityManager.java:21)
[18:51:14] [Thread-15/INFO] [STDERR]: [main.java.lidr.thread.LiDrThread:run:23]:    at java.lang.SecurityManager.checkExit(Unknown Source)
[18:51:14] [Thread-15/INFO] [STDERR]: [main.java.lidr.thread.LiDrThread:run:23]:    at javax.swing.JFrame.setDefaultCloseOperation(Unknown Source)
[18:51:14] [Thread-15/INFO] [STDERR]: [main.java.lidr.thread.LiDrThread:run:23]:    at main.java.lidr.thread.LightDrafterWindow.<init>(LightDrafterWindow.java:48)
[18:51:14] [Thread-15/INFO] [STDERR]: [main.java.lidr.thread.LiDrThread:run:23]:    at main.java.lidr.thread.LiDrThread.run(LiDrThread.java:18)

此时我不确定会出现什么问题。它似乎在生成的代码上失败了,而我对Gui的了解不够,看不出有什么问题。

如果您想在github

上看到更多我有此项目的代码

EDIT ------------ 在注释掉导致此错误的行之后,没有其他任何因素导致错误,但是jframe仍未显示。更新了代码以反映变化。

2 个答案:

答案 0 :(得分:1)

问题来自Forge代码。里面有代码阻止任何类调用系统退出。

解决方案是使用DISPOSE_ON_CLOSE。

答案 1 :(得分:0)

您必须将面板添加到JFrame,然后将其设置为可见。 没有框架你将看不到GUI。 最后添加这些行。

yourframe.add(contentPane);
yourframe.setvisible(true)
yourframe.pack();