当前位置: 代码迷 >> Java相关 >> 一个关于线程的问题
  详细解决方案

一个关于线程的问题

热度:248   发布时间:2006-06-11 20:24:50.0
一个关于线程的问题
我做这个题想了好久  但是不知道如何处理线程和文本框显示值的问题,请帮帮忙,附上源码  
搜索更多相关的解决方案: 线程  

----------------解决方案--------------------------------------------------------
不知道你的问题是什么
----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.applet.*;
import java.lang.InterruptedException;
public class KS10_24 extends JFrame
{
private JPanel panel;
private JPanel panel1;
private JPanel panel2;
private JButton jButton;
private JLabel lbl1;
private JLabel lbl2;
private JLabel lbl3;
private JLabel lbl4;
private JTextField txt1;
private JTextField txt2;
private JTextField txt3;
private boolean Bln = false;

public KS10_24()
{


panel = new JPanel();
panel1 = new JPanel();
panel2 = new JPanel();
jButton = new JButton("暂停");
lbl1 = new JLabel("模拟仪表盘");
lbl2 = new JLabel("100~200");
lbl3 = new JLabel("200~300");
lbl4 = new JLabel("500~600");
txt1 = new JTextField();
txt2 = new JTextField();
txt3 = new JTextField();

Container contentpane = getContentPane();

panel.setLayout(new FlowLayout());
panel.add(lbl1);
panel1.setLayout(new GridLayout(2,3));
panel1.add(txt1);
panel1.add(txt2);
panel1.add(txt3);
panel1.add(lbl2);
panel1.add(lbl3);
panel1.add(lbl4);
panel2.setLayout(new FlowLayout());
panel2.add(jButton);
final thread th = new thread(100,200,txt1);
final thread th1 = new thread(200,300,txt2);
final thread th2 = new thread(500,600,txt3);
th.start();
th1.start();
th2.start();
jButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{

if(Bln)
{
th.resume();
txt1.setText(th.txt.getText());
th1.resume();
txt2.setText(th.txt.getText());
th2.resume();
txt3.setText(th.txt.getText());
Bln = false;
jButton.setText("暂停");
}
else
{
th.suspend();
th1.suspend();
th2.suspend();
Bln = true;
jButton.setText("开始");
}

}
}
);


contentpane.add(panel,BorderLayout.NORTH);
contentpane.add(panel1,BorderLayout.CENTER);
contentpane.add(panel2,BorderLayout.SOUTH);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width - 300)/2,(screenSize.height - 300)/2);
setResizable(false);
setSize(300,200);
show();
}

public static void main(String[] args)throws Exception
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
KS10_24 th1 = new KS10_24();
}
}
class thread extends Thread
{
Random random = new Random();
int num,num2;
JTextField txt;
public thread(int number1,int number2,JTextField txt)
{
num = number1;
num2 = number2;
this.txt = txt;
}
public void run()
{
try
{
while(true)
{
txt.setText((int)(Math.random()*num2+num) + "");
sleep(500);
}
}
catch(InterruptedException e1)
{
}
}
public JTextField Return()
{
return txt;
}
}
我做出来了 不知道大家有有其他的做法 这是原码 但是还是有不足的地方` 如何产生100~200的随机数哦```
----------------解决方案--------------------------------------------------------

产生100-200的随机数还不简单
(int)(Math.random()*100+100);
就可以了


----------------解决方案--------------------------------------------------------
  相关解决方案