當前位置:編程學習大全網 - 編程語言 - java線程實現了壹個秒表不能結束?

java線程實現了壹個秒表不能結束?

主要是t = new Thread(this);

停止線程,不用調用stop,讓run()執行完就可以了

package testPro;

import java.awt.*;

import java.awt.event.*;

public class StopWatchFrame extends Thread

{

private Frame f;

private int ms;

private int ss;

private TextField tf;

private Font font;

private Thread t;

private Boolean flag;

Button b2 = new Button("End");

Button b1 = new Button("Begin");

public StopWatchFrame() {

ms = 0;

ss = 0;

flag = false;

t = new Thread(this);

f = new Frame("Sexxx's Stopwatch");

tf = new TextField();

font = new Font("微軟雅黑", Font.PLAIN, 100);

tf.setFont(font);

tf.setText("00:00");

tf.setSize(260, 120);

tf.setLocation(60, 60);

b1.setSize(50, 20);

b1.setLocation(400, 200);

b2.setSize(50, 20);

b2.setLocation(400, 250);

b1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

flag = true;

t.start();

}

});

b2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

flag = false;

}

});

f.setLayout(null);

f.setBounds(120, 120, 500, 300);

f.setBackground(Color.black);

f.add(tf);

f.add(b1);

f.add(b2);

f.setVisible(true);

f.setResizable(false);

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

Window w = e.getWindow();

w.dispose();

}

});

}

public void run() {

while (flag) {

ms++;

if (ms % 100 == 0) {

ms = 0;

ss++;

}

tf.setText((ss + "") + ":" + (ms + ""));

try {

Thread.sleep(10);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

public static void main(String[] args) {

new StopWatchFrame();

}

}

  • 上一篇:阜陽成效清北班和培優班是什麽時候分班的
  • 下一篇:二零壹六年九月500米口徑球面什麽在貴州平塘落成啟動
  • copyright 2024編程學習大全網