简体   繁体   English

在新线程中创建JFrame(新messageloog)

[英]Creating JFrame in a new Thread(new messageloog)

Hi i am trying to create multi JFrames but I want each one to has its own Thread (message loop) ,so when one JFrame freezes the others will keep working 嗨,我正在尝试创建多个JFrame,但我希望每个JFrame都有其自己的线程(消息循环),因此当一个JFrame冻结时,其他JFrame将继续工作

i tried to create each jframe from different Threads, but they are still working in the "AWT-EventQueue-0" thread. 我试图从不同的线程创建每个jframe,但是它们仍在“ AWT-EventQueue-0”线程中工作。

i come from a dotnet background. 我来自dotnet背景。 so when i want to do this scenario in a winForms app i usually call Application.run(new form()) from a new thread 因此,当我想在winForms应用程序中执行此方案时,通常会从新线程调用Application.run(new form())

can u please tell me how to do this in java ? 您能告诉我如何在Java中执行此操作吗?

thanks in advance! 提前致谢!

There is only UI thread in Java, no matter how many frames you open. Java中只有UI线程,无论打开多少帧。 I would suggest you to execute the long running operation within a thread. 我建议您在线程中执行长时间运行的操作。

public void actionPerformed(ActionEvent e)
{
    new Thread(new FrameRunnable()).start();
}

public class FrameRunnable implements Runnable
{
    public void run()
    {
        // Do stuff here
    }
}

Hope this will help. 希望这会有所帮助。

Read the section from the Swing tutorial on Concurrency to understand how the Event Dispatch Thread works. 阅读Swing 并发教程中的这一节,以了解事件调度线程是如何工作的。 All updates to GUI components must be done on the EDT. GUI组件的所有更新必须在EDT上完成。 If you have long running tasks, you can use a SwingWorker or a separate Thread along with SwingUtilities.invoke(...) later to add code to the EDT. 如果您的任务运行时间长,则可以稍后使用SwingWorker或单独的线程以及SwingUtilities.invoke(...)将代码添加到EDT。

Create a new thread for each JFrame generated. 为每个生成的JFrame创建一个新线程。 Take note of your Thread variables and pass it around on your Runnable. 记下您的Thread变量,并将其传递给您的Runnable。

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

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