java jtextfield 改变_java – 如何在更改变量值时更新JTextField?

我有两个Java(.java)文件.一个有JButton和JTextField,另一个有一个Thread.在第一个Java文件中,我向JButton添加了一个ActionListener,这样,当按下按钮时,一个线程(创建的第二个.java文件的对象和启动的线程)运行,它会连续修改整数变量.如何在JTextField(第一个.java文件)中显示该整数变量(第二个.java文件)的值?

Detection.java

package sample;

public class Detection implements Runnable

{

public String viewers;

public int count;

public void run()

{

try

{

while (true)

{

// i have written code for displaying video.

// and it say how many no. of people in the video

// the no of people is stored in a variable "count"

viewers=""+count; //storing count as string so as to display in the JTextField

}

}

catch (Exception e)

{

System.out.println("Exception: "+e);

}

}

}

UsrInterfac.java

//使用WindowBuilder构建eclipse juno

package sample;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JButton;

import javax.swing.JTextField;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class UsrInterfac

{

private JFrame frame;

private JTextField textField;

Detection dd = new Detection();

Thread th = new Thread(dd);

/**

* Launch the application.

*/

public static void main(String[] args)

{

EventQueue.invokeLater(new Runnable()

{

public void run()

{

try

{

UsrInterfac window = new UsrInterfac();

window.frame.setVisible(true);

}

catch (Exception e)

{

e.printStackTrace();

}

}

});

}

/**

* Create the application.

*/

public UsrInterfac()

{

initialize();

}

/**

* Initialize the contents of the frame.

*/

private void initialize()

{

frame = new JFrame();

frame.setBounds(100, 100, 450, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

JButton btnStartThread = new JButton("Start Thread");

btnStartThread.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent arg0)

{

th.start();

}

});

btnStartThread.setBounds(59, 133, 117, 23);

frame.getContentPane().add(btnStartThread);

textField = new JTextField();

textField.setBounds(270, 134, 104, 20);

frame.getContentPane().add(textField);

textField.setColumns(10);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值