| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 655 人关注过本帖
标题:[求助]郁闷,怎样退出这个循环
只看楼主 加入收藏
Public
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2005-9-9
收藏
 问题点数:0 回复次数:2 
[求助]郁闷,怎样退出这个循环

小弟初来乍到,遇到个难题请各位老师指教一下 请帮忙看一下这段代码,怎样退出下面的那个循环呢? 我的目的是:单击生成按钮,三个文本框生成随机数,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); } }

搜索更多相关主题的帖子: class public import 文本框 
2005-09-09 10:04
飘飘叶子
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:597
专家分:10
注 册:2005-8-17
收藏
得分:0 
北大青鸟的学生?
这题我做过的……先回复,待会再看代码。。。

向着软件工程师的目标前进!
2005-09-09 16:23
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分: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);
  }
}

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-10 02:44
快速回复:[求助]郁闷,怎样退出这个循环
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014709 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved