小弟初来乍到,遇到个难题请各位老师指教一下 请帮忙看一下这段代码,怎样退出下面的那个循环呢? 我的目的是:单击生成按钮,三个文本框生成随机数,2秒钟更新一次;单击暂停按钮,随机数停止生成,在单击生成按钮,恢复生成,请帮我看看是哪里出了问题了,暂停按钮无效 谢谢啦!!!
import java.awt.*; import java.awt.event.*;
class Playing { public static void main(String[] a) { Myframe mf = new Myframe(); mf.setSize(300,200); mf.show(true); } }
class Myframe extends Frame { Label l1 = new Label(); Label l2 = new Label("随机数",Label.CENTER); Label l3 = new Label(); Label l4 = new Label(); TextField t1 = new TextField("100-200"); TextField t2 = new TextField("200-300"); TextField t3 = new TextField("300-400"); TextField t4 = new TextField(7); TextField t5 = new TextField(7); TextField t6 = new TextField(7); Button b1 = new Button("生成"); Button b2 = new Button("暂停"); //窗口布局 public Myframe() { super("随机数测试"); setLayout(new GridLayout(4,3,15,15)); //网格布局 addWindowListener(new WinClosing()); add(l1); add(l2); add(l3); add(t1); add(t2); add(t3); //禁止输入 t1.setEditable(false); t2.setEditable(false); t3.setEditable(false); add(t4); add(t5); add(t6); add(b1); add(l4); add(b2); b2.enable(false); addWindowListener(new WinClosing()); MyActionListener ml1 = new MyActionListener(); b1.addActionListener(ml1); b2.addActionListener(ml1); } //按钮事件 private class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent ae) { boolean bo; DoRan dr = new DoRan(); if (ae.getSource() == b1) { System.out.println("开始"); bo = true; b1.enable(false); b2.enable(true); new MyThread(dr,bo); } else if (ae.getSource() == b2) { bo = false; b1.enable(true); b2.enable(false); new MyThread(dr,bo); } } } //新建线程 class MyThread implements Runnable { DoRan d; boolean b9; Thread t; public MyThread(DoRan dp,boolean x) { b9 = x; d = dp; t = new Thread(this); t.start(); } public void run() { d = new DoRan(b9); d.display(); } } //生成、显示随机数 class DoRan { int a,b,c; boolean bool; public DoRan() { } public DoRan(boolean bx) { bool = bx; } synchronized public void display() { for (int i=0;i<1;) //问题出在这里啊 { if(!bool) { System.out.println("暂停"); //可以运行到这一句,但是break好像不起作用,不解 break; } try { a=(int)(100.0+100.0*Math.random()); b=(int)(200.0+100.0*Math.random()); c=(int)(300.0+100.0*Math.random()); String s_a=Integer.toString(a); String s_b=Integer.toString(b); String s_c=Integer.toString(c); t4.setText(s_a); t5.setText(s_b); t6.setText(s_c); Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("错误,中断!!"); } } } } }
class WinClosing extends WindowAdapter {
public void windowClosing(WindowEvent e) { System.exit(0); } }
----------------解决方案--------------------------------------------------------
北大青鸟的学生?
这题我做过的……先回复,待会再看代码。。。
----------------解决方案--------------------------------------------------------
///////////////////////
//
// try it -:)
//
///////////////////////
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Playing
{
public static void main(String[] a)
{
Myframe mf = new Myframe();
mf.setSize(300,200);
mf.setVisible(true);
}
}
class Myframe extends Frame
{
Label l1 = new Label();
Label l2 = new Label("随机数",Label.CENTER);
Label l3 = new Label();
Label l4 = new Label();
TextField t1 = new TextField("100-200");
TextField t2 = new TextField("200-300");
TextField t3 = new TextField("300-400");
TextField t4 = new TextField(7);
TextField t5 = new TextField(7);
TextField t6 = new TextField(7);
JButton b1 = new JButton("生成");
JButton b2 = new JButton("暂停");
DoRan dr = new DoRan();
//窗口布局
public Myframe()
{
super("随机数测试");
setLayout(new GridLayout(4,3,15,15)); //网格布局
//DefaultCloseOperation(EXIT_ON_CLOSE);
//addWindowListener(new WinClosing());
add(l1); add(l2); add(l3);
add(t1); add(t2); add(t3);
//禁止输入
t1.setEditable(false);
t2.setEditable(false);
t3.setEditable(false);
add(t4); add(t5); add(t6);
add(b1); add(l4); add(b2);
b2.setEnabled(false);
addWindowListener(new WinClosing());
MyActionListener ml1 = new MyActionListener();
b1.addActionListener(ml1);
b2.addActionListener(ml1);
}
//按钮事件
private class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == b1)
{
System.out.println("开始");
b1.setEnabled(false);
b2.setEnabled(true);
dr.start();
}
else if (ae.getSource() == b2)
{
System.out.println("停止");
b1.setEnabled(true);
b2.setEnabled(false);
dr.stop();
}
}
}
//生成、显示随机数
class DoRan implements Runnable
{
int a,b,c;
Thread thread;
public DoRan()
{
}
public void start()
{
thread = new Thread(this);
thread.setName("Display");
thread.start();
}
public void stop()
{
thread = null;
}
public void run()
{
display();
}
synchronized public void display()
{
while(thread!=null)
//问题出在这里啊
{
try
{
a = (int) (100.0+100.0*Math.random());
b = (int) (200.0+100.0*Math.random());
c = (int) (300.0+100.0*Math.random());
String s_a=Integer.toString(a);
String s_b=Integer.toString(b);
String s_c=Integer.toString(c);
t4.setText(s_a);
t5.setText(s_b);
t6.setText(s_c);
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("错误,中断!!");
}
}
}
}
}
class WinClosing extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
----------------解决方案--------------------------------------------------------