【问题标题】:How to create JFrame from another main class as separate thread如何从另一个主类创建 JFrame 作为单独的线程
【发布时间】:2016-09-19 19:40:26
【问题描述】:

我想从另一个类(主类)创建一个 Jframe,然后我想仅在主方法中没有错误时才显示 Jframe。否则,我想通过传递相同的 jframe 来创建和显示对话框。 我在任何地方都忘记了线程概念给我解决方案,我尝试了下面的代码,打印“abcde”但不显示完整的框架程序。

注意:JFrame od ErrorDialog 中没有 main 方法。它们只是自定义容器。

public class Start{

    public static Main mf=null;

    public static void main(String args[]){

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                mf = new Main();
            }
        });
        try {
            // Some extra code
            System.out.println("abcde");     //  this is print and then program complete
            mf.setVisible(true);            //   this line will not run
        } catch (Exception e) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new ErrorDialog(mf, true).setVisible(true);;
                }
            });
        }
    }
}

【问题讨论】:

标签: java multithreading swing jframe main


【解决方案1】:

试试我的建议吧..

只需使用invokeAndWait() 方法而不是使用invokeLater() 方法。 因为invokeLater() 方法创建NullpointerException。 但invokeAndWait() 必须在try-catch 块内。

try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { mf = new Main(); } }); } catch (Exception e) { System.out.println(e); }

【讨论】:

    猜你喜欢
    • 2015-03-28
    • 2016-03-19
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多