娇眉恨
回答时间:2019-12-05
向TA提问
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestClock extends JFrame{
/** Creates a new instance of TestClock */
public TestClock() {
JPanel jp=new JPanel();
final JLabel jl=new JLabel("0");
jp.add(jl);
add(jp,BorderLayout.CENTER);
JButton jbStart=new JButton("开始");
jbStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton j =(JButton)e.getSource();
j.setEnabled(false);
dt=new DamThread(new ClockThread(jl));
dt.start();
}
});
JButton jbPause=new JButton("暂停");
jbPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton j=(JButton)e.getSource();
String s=(String)e.getActionCommand();
if(s.equals("暂停")){
dt.setStatus(ClockStatus.PAUSE);
j.setText("继续");
}else{
dt.setStatus(ClockStatus.CONTINUE);
j.setText("暂停");
}
}
});
JButton jbZero=new JButton("清零");
jbZero.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dt.setStatus(ClockStatus.ZERO);
}
});
JButton jbStop=new JButton("停止");
jbStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dt.setStatus(ClockStatus.STOP);
}
});
JPanel jp1=new JPanel();
jp1.add(jbStart);
jp1.add(jbPause);
jp1.add(jbZero);
jp1.add(jbStop);
add(jp1,BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
public static void main(String[] args) {
TestClock tc=new TestClock();
tc.setVisible(true);
}
DamThread dt;
}
class DamThread extends Thread{
public DamThread(ClockThread c){
this.ct=c;
ct.start();
this.setDaemon(true);
this.STATUS=ClockStatus.START;
}
public void run(){
while(ct.isAlive()){
CheckStatus();
}
}
收起
阅读全文