java 线程更新jframe,使用线程循环更新JFrame

ive done some extensive searching on using threads in a loop and whilst I understand the concept how how seperate threads work, I still cant seem to grasp how to implement it in my simple application.

My application consists of a form with a text box. This textbox needs to be updated once ever iteration of a loop. It starts with the press of a button but the loop should also finish with the press of a stop button. Ive used a boolean value to track if its been pressed.

Here is my form code:

package threadtester;

public class MainForm extends javax.swing.JFrame {

public MainForm() {

initComponents();

}

private void RunButtonActionPerformed(java.awt.event.ActionEvent evt) {

ThreadTester.setRunnable(true);

ThreadTester example = new ThreadTester(2,this);

example.run();

}

private void StopButtonActionPerformed(java.awt.event.ActionEvent evt) {

ThreadTester.setRunnable(false);

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new MainForm().setVisible(true);

}

});

}

public void setTextBox(String myString){

MainTextbox.setText(myString);

}

}

As you can see I have a button that is pressed. When the button is pressed this executes the code thats in a different class called ThreadTester. Here is the code for that class:

package threadtester;

import java.util.logging.Level;

import java.util.logging.Logger;

public class ThreadTester implements Runnable

{

int thisThread;

MainForm myMainForm;

private static boolean runnable;

// constructor

public ThreadTester (int number,MainForm mainForm)

{

thisThread = number;

myMainForm = mainForm;

}

public void run ()

{

for (int i =0;i< 20; i++) {

if(runnable==false){

break;

}

System.out.println("I'm in thread " + thisThread + " line " + i);

myMainForm.setTextBox(i + "counter");

try {

Thread.sleep(1000);

} catch (InterruptedException ex) {

Logger.getLogger(ThreadTester.class.getName()).log(Level.SEVERE, null, ex);

}

} }

public static void setRunnable(Boolean myValue){

runnable = myValue;

}

public static void main(String[] args) {

MainForm.main(args);

}

}

as you can see the loop has been created on a seperate thread... but the textbox only updates after the loop has finished. Now as far as im aware in my MainForm I created a seperate thread to run the loop on, so I dont understand why its not running? Any guidence would be much appreciated, ive tried looking at examples on stack exchange but I cant seem to get them to fit into my implemntation.

With the recommendation suggested by Tassos my run method now looks like this:

public void run ()

{

for (int i =0;i< 20; i++) {

if(runnable==false){

break;

}

System.out.println("I'm in thread " + thisThread + " line " + i);

final String var = i + "counter";

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

myMainForm.setTextBox(var);

}

});

try {

Thread.sleep(1000);

} catch (InterruptedException ex) {

Logger.getLogger(ThreadTester.class.getName()).log(Level.SEVERE, null, ex);

}

} }

解决方案

In order for Tassos' answer to work, you actually have to create an new thread, which you did not do. Simply calling

ThreadTester example = new ThreadTester(2,this);

example.run();

is not enough, sice that just calls the run method from EDT. You need to do the following:

Thread t = new Thread(new ThreadTester(2,this));

t.start();

Also, you want modify the same field from two different threads (runnable), which is a bug. You should read more about java concurrency.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值