Demo entry 6687276

java

   

Submitted by anonymous on Dec 27, 2017 at 07:26
Language: Java. Code size: 7.1 kB.

package 定时器_实验;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

@SuppressWarnings("serial")
public class TimerFrame extends JFrame implements ActionListener, CaretListener {
	private JTextField text[];
	private JLabel message, time;
	private JButton btn[];
	private Clock thread;
	private String str[] = { "天", "时", "分", "秒" };
	private int flag[][] = { { 1, 1, 1, 1, 4 }, { 0, 0, 0, 0 } };

	private class Clock extends Thread {
		private boolean isclean = false;// 是否复位
		private boolean iswait = false;// 是否等待
		private int dd, hh, mm, ss, ms;// 天、时、分、秒、毫秒

		public Clock(int dd, int hh, int mm, int ss) {
			this.dd = dd;
			this.hh = hh;
			this.mm = mm;
			this.ss = ss;
			this.ms = 0;
		}

		public synchronized void clean() {// 取消
			this.isclean = true;
			this.notify();
		}

		public void begin() {// 开始
			this.start();
		}

		public synchronized void pause() {// 暂停
			this.iswait = true;
		}

		public synchronized void continuing() {// 继续
			this.iswait = false;
			this.notify();
		}

		public void run() {
			int temp = dd + hh + mm + ss + ms;
			while ((!isclean) && temp != 0) {
				TimerFrame.this.time.setText(String.format("%02d  %02d:%02d:%02d:%02d", dd, hh, mm, ss, ms));
				int flag, flag1, flag2, flag3;
				flag = ((100 + ms - 1) / 100 + 1) % 2;
				ms = (100 + ms - 1) % 100;
				flag1 = ((60 + ss - flag) / 60 + 1) % 2;
				ss = (60 + ss - flag) % 60;
				flag2 = ((60 + mm - flag1) / 60 + 1) % 2;
				mm = (60 + mm - flag1) % 60;
				flag3 = ((24 + hh - flag2) / 24 + 1) % 2;
				hh = (24 + hh - flag2) % 24;
				dd = dd - flag3;
				try {
					Thread.sleep(10);
					synchronized (this) {
						if (iswait)
							wait();
					}
				} catch (InterruptedException e) {
				}
				temp = dd + hh + mm + ss + ms;
			}
			TimerFrame.this.time.setText(String.format("%02d  %02d:%02d:%02d:%02d", 0, 0, 0, 0, 0));
			if (0 == dd + hh + mm + ss + ms) {
				TimerFrame.this.btn[0].setText("开始");
				TimerFrame.this.btn[1].setText("取消");
				TimerFrame.this.btn[1].setEnabled(false);
				TimerFrame.this.btn[2].setEnabled(true);
				TimerFrame.this.message.setText("计时完成");
				for (int i = 0; i < TimerFrame.this.text.length; i++)
					TimerFrame.this.text[i].setEditable(true);
			}
		}
	}

	public TimerFrame() {
		super("倒计时");
		this.setSize(470, 200);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);

		this.setLayout(new GridLayout(4, 1));
		JPanel panel = new JPanel();
		this.add(panel);
		this.text = new JTextField[this.str.length];
		JLabel label[] = new JLabel[this.str.length];
		for (int i = 0; i < this.str.length; i++) {
			panel.add(this.text[i] = new JTextField("", 5));
			this.text[i].setHorizontalAlignment(JTextField.CENTER);
			this.text[i].addCaretListener(this);
			panel.add(label[i] = new JLabel(this.str[i]));
		}
		this.add(this.time = new JLabel("00 00:00:00:00", JLabel.CENTER));
		this.add(this.message = new JLabel("", JLabel.CENTER));
		JPanel panel_ = new JPanel();
		this.add(panel_);
		this.btn = new JButton[3];
		String s_btn[] = { "开始", "取消", "复位" };
		for (int i = 0; i < s_btn.length; i++) {
			panel_.add(this.btn[i] = new JButton(s_btn[i]));
			this.btn[i].addActionListener(this);
		}
		this.btn[1].setEnabled(false);

		this.setResizable(false);
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		if (e.getActionCommand().equals("开始")) {
			int flag1 = 1, flag2 = 0;
			String err = "";
			for (int j = 0; j < this.str.length; j++) {
				if (this.flag[0][j] == 0)
					err += (this.text[j].getText() + " " + this.str[j] + " ");
				flag1 *= this.flag[0][j];
				flag2 += this.flag[1][j];
			}
			if (flag1 == 0)
				this.message.setText("错误: " + err);
			else if (flag2 == 0)
				this.message.setText("时长不能为零");
			else {
				clockFormat();
				this.thread = new Clock(this.flag[1][0], this.flag[1][1], this.flag[1][2], this.flag[1][3]);
				this.thread.begin();
				this.btn[0].setText("暂停");
				this.btn[1].setEnabled(true);
				this.btn[2].setEnabled(false);
				this.message.setText("正在计时...");
				for (int i = 0; i < this.text.length; i++)
					this.text[i].setEditable(false);
			}
		} else if (e.getActionCommand().equals("暂停")) {
			this.thread.pause();
			this.btn[0].setText("继续");
			this.message.setText("暂停中...");
		} else if (e.getActionCommand().equals("继续")) {
			this.thread.continuing();
			this.btn[0].setText("暂停");
			this.message.setText("正在计时...");
		} else if (e.getActionCommand().equals("取消")) {
			this.thread.clean();
			for (int i = 0; i < this.text.length; i++)
				this.text[i].setEditable(true);
			this.message.setText("已取消");
			this.btn[0].setText("开始");
			this.btn[1].setEnabled(false);
			this.btn[2].setEnabled(true);
		} else {// 复位
			for (int i = 0; i < this.text.length; i++) {
				this.text[i].setText("");
			}
			this.message.setText("已复位。");
		}
	}

	@Override
	public void caretUpdate(CaretEvent e) {
		// TODO 自动生成的方法存根
		int index = 0, temp = -1;
		if (e.getSource() == this.text[0])
			index = 0;
		else if (e.getSource() == this.text[1])
			index = 1;
		else if (e.getSource() == this.text[2])
			index = 2;
		else
			index = 3;
		try {
			temp = Integer.parseInt(text[index].getText());
			if (temp > 0) {
				this.flag[1][index] = temp;
				message.setText("");
				this.flag[1][index] = temp;
				this.flag[0][index] = 1;
			} else if (temp == 0)
				throw new Exception("0");
			else
				throw new Exception("-1");
		} catch (NumberFormatException ex) {
			if (text[index].getText().equals("")) {
				this.flag[1][index] = 0;
				this.flag[0][index] = 1;
				message.setText("");
			} else {
				message.setText("错误: \"" + text[index].getText() + "\"");
				this.flag[0][index] = 0;
			}
		} catch (Exception ex) {
			if (text[index].getText().equals("") || temp == 0) {
				this.flag[1][index] = 0;
				this.flag[0][index] = 1;
				message.setText("");
			} else if (temp < 0) {
				this.flag[0][index] = 0;
				message.setText("错误: \"" + text[index].getText() + "\"");
			}
		}
	}

	public void clockFormat()// 把所有文本框的数值格式化
	{
		int flag[] = { 0, 0, 0 };
		flag[0] = this.flag[1][3] / 60;
		this.flag[1][3] = this.flag[1][3] % 60;// 格式化 秒
		flag[1] = (this.flag[1][2] + flag[0]) / 60;
		this.flag[1][2] = (this.flag[1][2] + flag[0]) % 60;// 格式化 分
		flag[2] = (this.flag[1][1] + flag[1]) / 24;
		this.flag[1][1] = (this.flag[1][1] + flag[1]) % 24;// 格式化 时
		this.flag[1][0] = this.flag[1][0] + flag[2];// 格式化 天
		for (int i = 0; i < this.text.length; i++)
			this.text[i].setText("" + this.flag[1][i]);
	}

	public static void main(String[] args) {
		new TimerFrame();
	}
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).