我有两个Java(.java) files
。一个有JButton
和JTextField
,另一个有Thread
。在第一个Java file
中,我添加了一个ActionListener
到JButton
,这样当按下按钮时,一个线程(第二个.java文件的对象在已创建的线程中启动)运行,它连续修改整型变量。如何在第一个.java文件的JTextField
(第二个.java文件)中显示该整数变量的值?如何在变量值更改时更新JTextField?
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日食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);
}
}
这是很难理解你想要什么,尝试用一些样本解释代码,比如'Class Frame'具有'JButton'和'JTextField'。 '类处理器'想访问一些数据,像这样的 – Apurv 2013-03-28 06:58:31
我已经附上了代码。我想将Detection.java中的变量“查看者”的值显示在UsrInterfac.java的“textField”中,并告诉我如何执行此操作。另外,当我们在线程中传输视频时,变量“观众”不断变化。 – 2013-03-28 07:35:17