📄 test2.java
字号:
package com.xinxi.test;
import java.awt.*;
import java.awt.event.*;
public class Test2 extends Frame {
Label lb = null;
Button btBegin = null;
Button btEnd = null;
long li = 0;
boolean bflag = false;
public Test2(){
}
public static void main(String[] args) {
new Test2().launch();
}
public void launch(){
this.setSize(300, 300);
this.setLocation(100,100);
lb = new Label("0");
btBegin = new Button("开始");
btEnd = new Button("结束");
lb.setSize(200, 30);
add(lb, BorderLayout.NORTH);
Panel p = new Panel();
p.add(btBegin, BorderLayout.WEST);
p.add(btEnd, BorderLayout.EAST);
add(p, BorderLayout.SOUTH);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
btBegin.addActionListener(new MyListener());
btEnd.addActionListener(new MyListener());
this.setVisible(true);
}
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
Button b = (Button)o;
if(b == btEnd){
try {
bflag = true;
wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} else if(b == btBegin){
if(bflag == false){
MyThread mt = new MyThread();
new Thread(mt).start();
} else {
notifyAll();
}
}
}
}
class MyThread implements Runnable {
public void run() {
while(true){
if(li == 65535){
li = 0;
} else {
lb.setText(String.valueOf(li));
}
li++;
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -