import javax.swing.*;
import java.awt.Toolkit;public class TimeFrame extends JFrame implements Runnable {
private JLabel lbl;
private JPanel p;
private Thread t;
public TimeFrame() {
p = new JPanel();
lbl = new JLabel();
p.add(lbl);
getContentPane().add(p);
t = new Thread(this);
t.start();
}
public void run() {
int hour = 0;
int minute = 0;
int second = 5;
try {
while(true) {
lbl.setText(String.valueOf(hour + \" : \" + minute + \" : \" + second));
if(second == 0) {
second = 59; //如果秒为0,则改为59,否则自减
if(minute == 0) {
minute = 59; //如果分为0,则改为59,否则自减
if(hour == 0) {
break; //如果小时为0,则退出,并发出\"吡\"的声音
}
else
hour--;
}
else
minute--;
}
else
second--;
t.sleep(1000);
}
Toolkit.getDefaultToolkit().beep();
}
catch(Exception e) {
}
}
public static void main(String[] args) {
TimeFrame f = new TimeFrame();
f.setTitle(\"倒计时\");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(100,100);
f.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit;public class TimeFrame extends JFrame implements Runnable,ActionListener {
private JLabel lbl;private JPanel p;
private Thread t;
private JButton editButton,startButton;
private DialogFrame dialog;
private int hour = 2,minute = 0,second = 0;public TimeFrame() {
p = new JPanel();
editButton = new JButton(\"修改时间\");
startButton = new JButton(\"开始倒计时\");
editButton.addActionListener(this);
startButton.addActionListener(this);
lbl = new JLabel(hour + \" : \" + minute + \" : \" + second);
p.add(lbl);
p.add(editButton);
p.add(startButton);
getContentPane().add(p);
}public void run() {
try {
while(true) {
lbl.setText(hour + \" : \" + minute + \" : \" + second);
if(second == 0) {
second = 59; //如果秒为0,则改为59,否则自减
if(minute == 0) {
minute = 59; //如果分为0,则改为59,否则自减
if(hour == 0) {
startButton.setText(\"开始倒计时\");
break; //如果小时为0,则退出,并发出\"吡\"的声音
}
else
hour--;
}
else
minute--;
}
else
second--;
t.sleep(1000);
}
Toolkit.getDefaultToolkit().beep();
}
catch(Exception e) {
}
}
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if(o == editButton) {
dialog = new DialogFrame(this);
}
else if(o == startButton) {
String text = startButton.getText();
if(\"开始倒计时\".equals(text)) {
t = new Thread(this);
t.start();
startButton.setText(\"停止倒计时\");
}
else {
t.stop();
startButton.setText(\"开始倒计时\");
}
}
}public static void main(String[] args) {
TimeFrame f = new TimeFrame();
f.setTitle(\"倒计时\");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200,200);
f.setVisible(true);
}
public void setTime(int hour,int min,int sec) {
this.hour = hour;
this.minute = min;
this.second = sec;
lbl.setText(hour + \" : \" + minute + \" : \" + second);
}
}class DialogFrame extends JDialog implements ActionListener {
private JTextField hourText,minText,secText;
private JButton okButton,canceButton;
private TimeFrame f;
public DialogFrame(TimeFrame f) {
super(f,\"修改时间\");
this.f = f;
hourText = new JTextField(5);
minText = new JTextField(5);
secText = new JTextField(5);
okButton = new JButton(\"OK\");
canceButton = new JButton(\"Cance\");
okButton.addActionListener(this);
canceButton.addActionListener(this);
setLayout(new FlowLayout());
add(hourText);
add(minText);
add(secText);
add(okButton);
add(canceButton);
setSize(200,100);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if(o == okButton) {
try {
int hour = Integer.valueOf(hourText.getText());
int min = Integer.valueOf(minText.getText());
int sec = Integer.valueOf(secText.getText());
f.setTime(hour,min,sec);
}
catch(Exception ex) {
//如果输入的不是数字,在这里进行处理
}
dispose();
}
else if(o == canceButton) {
dispose();
}
}
}