简体   繁体   English

如何在 main 仍在运行时使用线程运行另一个 JFrame

[英]How to use a thread to run another JFrame while the main is still running

I have a jframe i want to display while my main frame is running.我有一个 jframe 我想在我的主框架运行时显示。 i want to pause my main code, until the user does the necessary actions on the other frame.我想暂停我的主要代码,直到用户在另一帧上执行必要的操作。 I've read a lot of solutions but i need to see it done for my code to understand and grasp it fully.我已经阅读了很多解决方案,但我需要看到它完成了我的代码才能完全理解和掌握它。 i do not want to use jdialog like I've seen listed as an answer before.我不想像以前看到的那样使用 jdialog 作为答案。 My main goal is to understand better threading so that i can use what i learn in different cases.我的主要目标是更好地理解线程,以便我可以在不同的情况下使用我学到的东西。

With the code I've created, when running the thread, only just the frame loads, none of the other features are there on the frame.使用我创建的代码,在运行线程时,只加载框架,框架上没有其他功能。 (the frame is simple it has a label, a list the user selects from, and a button to basically return the chosen list value.) its like the thread is cut off from completing or something. (框架很简单,它有一个标签,一个用户从中选择的列表,以及一个基本上返回所选列表值的按钮。)它就像线程被切断完成或其他东西。

here is my class calling the screen:这是我的班级调用屏幕:

public class myThread implements Runnable {

    String result = null;

    public void run() {
        MessageScreen ms = new MessageScreen();
        ms.setVisible(true);
    }

    public String getResult() {
        return result;
    }

    public void setResult(String AS) {
        result = AS;
    }

}

in my main code, a method is called that is returning a String[] value, with this method at some point i have the following code calling the new thread to get the value necessary to return in the original main method:在我的主代码中,调用了一个返回 String[] 值的方法,在某些时候使用此方法,我有以下代码调用新线程以获取在原始主方法中返回所需的值:

    myThread mt = new myThread();
    Thread t = new Thread(mt);
    t.start();
    try {
        t.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    myreturn = new String[] {"true", mt.getResult()}; 

without listing the whole code for the second frame, when the user presses the button, and at the end of the listener tied to the button press the i want to close the frame and return a string that was selected from the list:没有列出第二帧的整个代码,当用户按下按钮时,在绑定到按钮的侦听器的末尾按下我想关闭框架并返回从列表中选择的字符串:

jf.dispose();
myt.setResult(AdminSelection);

in the frame class, i have the following instance variables declared:在框架类中,我声明了以下实例变量:

String AdminSelection = null;
myThread myt; 

i hope this is enough information for someone to help me out and understand whats gone wrong here.我希望这是足够的信息,有人可以帮助我并了解这里出了什么问题。

The function join() waits until the end of the run() method, when you do t.join() , your thread is already or almost ended.函数join()一直等到run()方法结束,当您执行t.join()时,您的线程已经或几乎结束。 This is because in your run() method there is nothing that blocks the thread until the user has clicked the confirm button.这是因为在您的run()方法中,在用户单击确认按钮之前,没有任何东西会阻塞线程。 And is better like this!最好这样!

There is no sense to create a thread here, you should use a callback, or more generally in Java, a listener.在这里创建线程是没有意义的,你应该使用回调,或者在 Java 中更一般地,一个监听器。 You can take a look at Creating Custom Listeners .您可以查看 创建自定义侦听器


But, especially if you want to pause your main code, you should use a (modal) JDialog which is made for this!但是,特别是如果你想暂停你的主代码,你应该使用一个(模态的) JDialog ,它是为此而设计的! Don't try to block the UI by yourself, you could block the UI thread (handled by Swing/AWT) by mistake.不要试图自己阻塞 UI,你可能会错误地阻塞 UI 线程(由 Swing/AWT 处理)。 Creating a JDialog is better because everything is already made for this usage on the UI thread.创建一个JDialog会更好,因为在 UI 线程上已经为这种用法做好了一切准备。

Also, you must know that create a Thread is really long, use a Thread when you really need it.另外,你必须知道创建一个Thread真的很长,在你真正需要的Thread使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM