简体   繁体   English

从线程更新Swing GUI

[英]Updating Swing GUI from thread

I have a simple application with some threads, I need tu update SWING GUI (I'm using Netbeans) from some thread. 我有一个带有一些线程的简单应用程序,我需要从某个线程更新SWING GUI(我正在使用Netbeans)。

This is my main form which i need to update: 这是我需要更新的主要表格:

public class mainForm extends javax.swing.JFrame {
private gameControll obj;
  public mainForm() {

    initComponents();
}
 public void runAuto(){
ThreadTest th1 = new ThreadTest(1, 1, obj);
ThreadTest th8 = new ThreadTest(8, 95000, obj);
ThreadTest th2 = new ThreadTest(2, 100000, obj);
ThreadTest th3 = new ThreadTest(3, 120000, obj);
ThreadTest th4 = new ThreadTest(4, 140000, obj);
ThreadTest th22 = new ThreadTest(22, 1000, obj);

Thread thread1 = new Thread(th1);
Thread thread2 = new Thread(th2);
Thread thread3 = new Thread(th3);
Thread thread4 = new Thread(th4);
Thread thread22 = new Thread(th22);
Thread thread8 = new Thread(th8);

thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread22.start();
thread8.start();

}
public static void main(String args[]) {

SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    mainForm app = new mainForm();
    app.setVisible(true);
        }
    });
}
    private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField pathtbox;                 
}

Now i have some threads: 现在我有一些线程:

public class ThreadTest implements Runnable {
public static mainForm main = new mainForm();
private final int functionNumber;
private final int time2start;
public ThreadTest(int functionNumber, int time2start, gameControll obj){
this.functionNumber = functionNumber;
this.time2start = time2start;

}

   @Override
   public void run(){

try{Thread.sleep(time2start);}catch(Exception ex){}//Time Delay before executing methods
switch(functionNumber){
    case 1:
//System.out.println("case 1");
        obj.runFirst();
        break;
    case 2:
   //     System.out.println("case 2");
        obj.runSecond();

        break;
    case 3:

{
    try {
        obj.runThird();
    } catch (IOException ex) {
        Logger.getLogger(ThreadTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}
        break;
...

ANd in some other class i have: 我还有其他班级:

  public void runFirst(){
   System.out.println("I need to show this text on jlabel5");
   }

Each part of the code is in different file (class). 代码的每个部分都在不同的文件(类)中。 How would i implement Swing worker here to be able to show text on my main GUI form? 我将如何在此处实施Swing Worker以便在主GUI表单上显示文本?

How would i implement Swing worker here to be able to show text on my main GUI form? 我将如何在此处实施Swing Worker以便在主GUI表单上显示文本?

Based on this list of features observed in your code: 基于代码中观察到的以下功能列表:

  • You want an initial delay to perform tasks. 您希望执行任务时出现最初的延迟。
  • Tasks are intended to be performed in a sequential order. 任务应按顺序执行。
  • Tasks are intended to update GUI components but not to perform heavy tasks (ie: set a label's text). 任务旨在更新GUI组件,但不执行繁重的任务(即:设置标签的文本)。

IMO Swing Timer suits better than Swing worker in this case. 在这种情况下,IMO Swing Timer比Swing工人更合适。 Instead of having a class implementing Runnable interface you could define several timers with access to the MainForm class instance in order to perform the desired actions and update the very same form (right now you create a new form as a class member of ThreadTest class). 可以不用定义一个类来实现Runnable接口,而是可以定义几个可以访问MainForm类实例的计时器,以执行所需的操作并更新完全相同的表单(现在,您将创建一个新表单作为ThreadTest类的类成员)。 For example: 例如:

    Timer timer1 = new Timer(1, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            mainFrame.runFirst();
        }
    });
    timer1.setRepeats(false);

    Timer timer2 = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            mainFrame.runSecond();
        }
    });
    timer2.setRepeats(false);
    ...
    timer1.start();
    timer2.start();
    ...

Where MainFrame#runFirst() should look like this: MainFrame#runFirst()应该看起来像这样:

public void runFirst() {
    jlabel5.setText("Label#5 updated from Timer!");
}

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

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