我的意思是单击“start”后,再单击“start”后重新启动(记数)
代码如下:怎么改?谢谢import java.awt.*;
import java.awt.event.*;
import java.applet.*;public class Toolbar extends Applet implements Runnable,ActionListener
{
private int count = 0;
private Button
onOff = new Button("Toggle"),
start = new Button("Start");
private TextField t = new TextField(10);
private boolean runFlag = true;
Thread thread;
public void init() {
// thread = new Thread(this,"animation");
add(t);
start.addActionListener(this);
add(start);
onOff.addActionListener(this);
add(onOff);
// thread.start();
}
public void run() {
while (true) {
try {
Thread.sleep(100);
if(runFlag)
t.setText(Integer.toString(count++)); }catch (InterruptedException e){}
}
}
public static void main(String[] args) {
Toolbar applet = new Toolbar();
Frame aFrame = new Frame("Counter1");
aFrame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(300,200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int i=0;
if(e.getActionCommand()=="Start")
{
if(thread !=null)
{
thread.destroy();
thread.start();
}
else
thread = new Thread(this,"a");
thread.start();
}
if(e.getActionCommand()=="Toggle")
{
if(thread != null)
runFlag = !runFlag;
}
}
} ///:~
代码如下:怎么改?谢谢import java.awt.*;
import java.awt.event.*;
import java.applet.*;public class Toolbar extends Applet implements Runnable,ActionListener
{
private int count = 0;
private Button
onOff = new Button("Toggle"),
start = new Button("Start");
private TextField t = new TextField(10);
private boolean runFlag = true;
Thread thread;
public void init() {
// thread = new Thread(this,"animation");
add(t);
start.addActionListener(this);
add(start);
onOff.addActionListener(this);
add(onOff);
// thread.start();
}
public void run() {
while (true) {
try {
Thread.sleep(100);
if(runFlag)
t.setText(Integer.toString(count++)); }catch (InterruptedException e){}
}
}
public static void main(String[] args) {
Toolbar applet = new Toolbar();
Frame aFrame = new Frame("Counter1");
aFrame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(300,200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int i=0;
if(e.getActionCommand()=="Start")
{
if(thread !=null)
{
thread.destroy();
thread.start();
}
else
thread = new Thread(this,"a");
thread.start();
}
if(e.getActionCommand()=="Toggle")
{
if(thread != null)
runFlag = !runFlag;
}
}
} ///:~
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货