该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Clock extends JFrame {
private static final long serialVersionUID = 1L;
private Dialog dialog;
public static void main(String[] args) { //主方法
Clock f = new Clock();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
class MyDialog extends Dialog implements WindowListener, ActionListener {
private static final long serialVersionUID = 1L;
JLabel label;
JPanel panel1, panel2;
JButton button;
public MyDialog(Frame owner, String title, boolean modal) {// MyDialog的构造方法
super(owner, title, modal);
label = new JLabel("时间到!");//面板的定义
button = new JButton("确定");
panel1 = new JPanel();
panel2 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add("Center", label);
panel2.add("Center", button);
this.add("Center", panel1);
this.add("South", panel2);
this.setSize(800, 800);
this.setResizable(false);
this.addWindowListener(this);
button.addActionListener(this);
}
//定义各类接口的方法,没有方法体
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
this.setVisible(false);
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void actionPerformed(ActionEvent e) { //设置动作
this.setVisible(false);
}
}
Clock() { //Clock的构造方法
setTitle("倒计时");
setSize(920, 820);
dialog = new MyDialog(this, "提示:", true);
ClockPanel p = new ClockPanel();
add(p);
}
class ClockPanel extends JPanel { //构造Clock的面板
private static final long serialVersionUID = 1L;
private JButton b;
private boolean onetime = true;;
private JLabel lfen, lmiao, l;
private JTextField tf, tm;
ClockPanel() { //ClockPanel的构造方法
b = new JButton("开始倒计时");
lfen = new JLabel("分");
lmiao = new JLabel("秒");
l = new JLabel("00:00");
tf = new JTextField(3);
tm = new JTextField(3);
l.setFont(new Font("宋体", Font.BOLD, 300));
l.setForeground(Color.RED);
b.addActionListener(new ActionListener() { //设置监听器
public void actionPerformed(ActionEvent arg0) {
if (onetime) {
if (tf.getText().trim().equals("")) {
tf.setText("00");
}
if (tm.getText().trim().equals("")) {
tm.setText("00");
}
new ChangeLabel(tf.getText().trim() + ":"
+ tm.getText().trim()).start();
}
}
});
add(tf);
add(lfen);
add(tm);
add(lmiao);
add(b);
add(l);
}
class ChangeLabel extends Thread // 运行秒针线程
{
private int minitues;
private String Sminitues;
private int sound;
private String Ssound;
private String LabelTime;
public ChangeLabel(String time) { //构造方法
onetime = false;
this.minitues = Integer.parseInt(time.substring(0, time
.indexOf(':')));
this.sound = Integer.parseInt(time
.substring(time.indexOf(':') + 1));
}
private long time1;
private long time2;
public void run() { //定义线程Thread的run方法
time1 = System.currentTimeMillis();
while (true) {
time2 = System.currentTimeMillis();
while (!(minitues == 0 & sound == 0)
&& time2 >= time1 + 1000) {
time1 = time2;
if (sound == 0) {
sound = 59;
minitues--;
} else {
sound--;
}
LabelTime = this.getTime();
display();
}
if (minitues == 0 && sound == 0) {
dialog.setVisible(true);
onetime = true;
break;
}
}
}
private String getTime() {
if (minitues < 10)
this.Sminitues = "0" + minitues;
else
this.Sminitues = "" + minitues;
if (sound < 10)
this.Ssound = "0" + sound;
else
this.Ssound = "" + sound;
return this.Sminitues + ":" + this.Ssound;
}
public void display() { //显示倒计时
l.setText(this.LabelTime);
}
}
}
}