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

小弟初来乍到,遇到个难题请各位老师指教一下 请帮忙看一下这段代码,怎样退出下面的那个循环呢? 我的目的是:单击生成按钮,三个文本框生成随机数,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
快速回复:[求助]郁闷,怎样退出这个循环
数据加载中...
 
   



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

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