import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Counter1
{
public static void main(String[] args)
{
CountFrame aFrame = new CountFrame();
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aFrame.show();
}
}class CountFrame extends JFrame
{
public CountFrame()
{
setTitle("Count");
setSize(300,300);
Container content = getContentPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
t = new TextField();
panel1.add(t);
start = new Button("start");
start.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
run = !run;
while(true)
{
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {System.out.println("error");}
if(run)
t.setText(Integer.toString(count++));
}
}
}
);
exit = new Button("stop");
exit.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
panel2.add(start);
panel2.add(exit);
content.add(panel1,BorderLayout.WEST);
content.add(panel2,BorderLayout.EAST);
}
boolean run = true;
private int count = 0;
private TextField t;
private Button start,exit;
}
import java.awt.event.*;
import javax.swing.*;
public class Counter1
{
public static void main(String[] args)
{
CountFrame aFrame = new CountFrame();
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aFrame.show();
}
}class CountFrame extends JFrame
{
public CountFrame()
{
setTitle("Count");
setSize(300,300);
Container content = getContentPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
t = new TextField();
panel1.add(t);
start = new Button("start");
start.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
run = !run;
while(true)
{
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {System.out.println("error");}
if(run)
t.setText(Integer.toString(count++));
}
}
}
);
exit = new Button("stop");
exit.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
panel2.add(start);
panel2.add(exit);
content.add(panel1,BorderLayout.WEST);
content.add(panel2,BorderLayout.EAST);
}
boolean run = true;
private int count = 0;
private TextField t;
private Button start,exit;
}
解决方案 »
- IO求高手详细注释!
- struts2下的“Exception starting filter struts2”错误的问题
- System.out.println()
- 求Boolean类中toString方法的使用简单例子
- 这样用sql取数据很不理解
- 如何让程序在繁忙的时候把鼠标变为沙漏?
- 请问jbuilder9怎样使用jdk1.42呢
- 我的程序能实现题目要求就是出现了异常 怎么解决异常呀 ???请人解决
- 有没有了解创智情况的朋友?想去创智,了解一下情况先!
- 怎么根据Comparable方法中的compareTo方法的返回值的正负 判断升序 还是 降序?
- 【求助】如何遍历一个二叉树!(老总快给我逼疯了,急啊)
- 如何调整TextField的高度?
{
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {System.out.println("error");}
if(run)
t.setText(Integer.toString(count++));
}
你这个循环没有退出条件,建议你设置一个boolean变量flag,while(flag)代替while(true),
然后在循环体内在一定情况下设置flag=false.