想法
用一個JFrame裡面放三個Tab panel分別為
時間、計時、倒數
利用三個Thread控制以上三件事
1時間
取得一個Calendar.getInstance() 得到 時、分、秒
然後在Thread的Run裡每一秒加一
2計時
”開始”利用一個Runnable從0時0分0秒一直累加
”結束”把Runnable解除掉(run直接結束)
”清除”設時、分、秒為0
3倒數
需三個TextField控制設定初始之時、分、秒
”開始”利用一個Runnable初始值一直減少到0
”結束”把Runnable解除掉(run直接結束)
會有的問題
1. 利用Sleep控制會因為電腦的處理其它判斷快慢而影響其正確性
2. 倒數之設定初始值可用除制只輸入數字及判斷輸入值之正確性來避免Exception
3. 計時及倒數,初始時並沒有顯示於畫面上
以下是程式碼及包裝好的jar檔,有興趣的可以下載看看
展畫面如下
時鐘

計時器


倒數計時器


原始碼:
package test.clock;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Dimension;
import javax.swing.JTabbedPane;
import java.awt.ComponentOrientation;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Clock extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel Time_h = null;
private JTabbedPane menu = null;
private JPanel Clock_1 = null;
private JPanel Clock_2 = null;
private JPanel Clock_3 = null;
private JLabel Time_up = null;
private JButton Up_Start = null;
private JButton Up_Stop = null;
private JButton Up_Clear = null;
private DownTimeThread downTimeThread = null;
private UpTimeThread upTimeThread = null;
private JLabel Time_down = null;
private JButton Down_Start = null;
private JButton Down_Stop = null;
private JTextField hh = null;
private JTextField mm = null;
private JTextField ss = null;
/**
* This is the default constructor
*/
public Clock() {
super();
initialize();
(new NowTimeThread(this)).start();// 取得現在時間
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 189);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.setVisible(true);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
Time_h = new JLabel();
Time_h.setText("");
Time_h.setBounds(new Rectangle(37, 38, 177, 48));
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getMenu(), null);
}
return jContentPane;
}
/**
* This method initializes menu
*
* @return javax.swing.JTabbedPane
*/
private JTabbedPane getMenu() {
if (menu == null) {
menu = new JTabbedPane();
menu.setBounds(new Rectangle(4, 2, 280, 149));
menu.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
menu.addTab("時鐘", null, getClock_1(), null);
menu.addTab("計時", null, getClock_2(), null);
menu.addTab("倒數", null, getClock_3(), null);
}
return menu;
}
/**
* This method initializes Clock_1
*
* @return javax.swing.JPanel
*/
private JPanel getClock_1() {
if (Clock_1 == null) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
Clock_1 = new JPanel();
Clock_1.setLayout(null);
Clock_1.add(Time_h, null);
}
return Clock_1;
}
/**
* This method initializes Clock_2
*
* @return javax.swing.JPanel
*/
private JPanel getClock_2() {
if (Clock_2 == null) {
Time_up = new JLabel();
Time_up.setBounds(new Rectangle(9, 23, 124, 35));
Time_up.setText("");
Clock_2 = new JPanel();
Clock_2.setLayout(null);
Clock_2.add(Time_up, null);
Clock_2.add(getUp_Start(), null);
Clock_2.add(getUp_Stop(), null);
Clock_2.add(getUp_Clear(), null);
}
return Clock_2;
}
/**
* This method initializes Clock_3
*
* @return javax.swing.JPanel
*/
private JPanel getClock_3() {
if (Clock_3 == null) {
Time_down = new JLabel();
Time_down.setBounds(new Rectangle(12, 29, 123, 34));
Time_down.setText("");
Clock_3 = new JPanel();
Clock_3.setLayout(null);
Clock_3.add(Time_down, null);
Clock_3.add(getDown_Start(), null);
Clock_3.add(getDown_Stop(), null);
Clock_3.add(getHh(), null);
Clock_3.add(getMm(), null);
Clock_3.add(getSs(), null);
}
return Clock_3;
}
public void setTime(int type, String value) {
if (type == 1)
Time_h.setText(value);
else if (type == 2)
Time_up.setText(value);
else if (type == 3)
Time_down.setText(value);
// int uuu=1;
// Time_Up.
}
/**
* This method initializes Up_Start
*
* @return javax.swing.JButton
*/
private JButton getUp_Start() {
if (Up_Start == null) {
Up_Start = new JButton();
Up_Start.setBounds(new Rectangle(156, 13, 93, 26));
Up_Start.setText("開始");
Up_Start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// System.out.println("Start");
getUpTimeThread().setStart();
// System.out.println("End");
Thread t = new Thread(getUpTimeThread());
t.start();
getUp_Start().setEnabled(false);
getUp_Stop().setEnabled(true);
}
});
}
return Up_Start;
}
/**
* This method initializes Up_Stop
*
* @return javax.swing.JButton
*/
private JButton getUp_Stop() {
if (Up_Stop == null) {
Up_Stop = new JButton();
Up_Stop.setBounds(new Rectangle(156, 46, 93, 25));
Up_Stop.setEnabled(false);
Up_Stop.setText("結束");
Up_Stop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
getUpTimeThread().setStop();
getUp_Start().setEnabled(true);
getUp_Stop().setEnabled(false);
}
});
}
return Up_Stop;
}
/**
* This method initializes Up_Clear
*
* @return javax.swing.JButton
*/
private JButton getUp_Clear() {
if (Up_Clear == null) {
Up_Clear = new JButton();
Up_Clear.setBounds(new Rectangle(155, 77, 93, 25));
Up_Clear.setText("清空");
Up_Clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
getUpTimeThread().setStop();
getUpTimeThread().setClear();
getUp_Start().setEnabled(false);
getUp_Stop().setEnabled(true);
}
});
}
return Up_Clear;
}
/**
* This method initializes downTimeThread
*
* @return test.clock.DownTimeThread
*/
private DownTimeThread getDownTimeThread() {
if (downTimeThread == null) {
downTimeThread = new DownTimeThread();
downTimeThread.init(this);
}
return downTimeThread;
}
/**
* This method initializes upTimeThread
*
* @return test.clock.UpTimeThread
*/
private UpTimeThread getUpTimeThread() {
if (upTimeThread == null) {
upTimeThread = new UpTimeThread();
upTimeThread.init(this);
}
return upTimeThread;
}
/**
* This method initializes Down_Start
*
* @return javax.swing.JButton
*/
private JButton getDown_Start() {
if (Down_Start == null) {
Down_Start = new JButton();
Down_Start.setBounds(new Rectangle(155, 7, 93, 30));
Down_Start.setText("開始");
Down_Start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
int h = Integer.parseInt(getHh().getText());
int m = Integer.parseInt(getMm().getText());
int s = Integer.parseInt(getSs().getText());
// System.out.println("Start");
getDownTimeThread().setTime(h, m, s);
getDownTimeThread().setStart();
// System.out.println("End");
Thread t = new Thread(getDownTimeThread());
t.start();
getDown_Start().setEnabled(false);
getDown_Stop().setEnabled(true);
}
});
}
return Down_Start;
}
/**
* This method initializes Down_Stop
*
* @return javax.swing.JButton
*/
private JButton getDown_Stop() {
if (Down_Stop == null) {
Down_Stop = new JButton();
Down_Stop.setBounds(new Rectangle(154, 42, 96, 33));
Down_Stop.setEnabled(false);
Down_Stop.setText("結束");
Down_Stop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
getDownTimeThread().setStop();
getDown_Start().setEnabled(true);
getDown_Stop().setEnabled(false);
}
});
}
return Down_Stop;
}
/**
* This method initializes hh
*
* @return javax.swing.JTextField
*/
private JTextField getHh() {
if (hh == null) {
hh = new JTextField();
hh.setBounds(new Rectangle(75, 82, 47, 25));
hh.setText("0");
}
return hh;
}
/**
* This method initializes mm
*
* @return javax.swing.JTextField
*/
private JTextField getMm() {
if (mm == null) {
mm = new JTextField();
mm.setBounds(new Rectangle(130, 82, 47, 25));
mm.setText("2");
}
return mm;
}
/**
* This method initializes ss
*
* @return javax.swing.JTextField
*/
private JTextField getSs() {
if (ss == null) {
ss = new JTextField();
ss.setBounds(new Rectangle(187, 82, 52, 28));
ss.setText("23");
}
return ss;
}
public static void main(String args[]) {
new Clock();
}
} // @jve:decl-index=0:visual-constraint="10,10"
package test.clock;
import java.util.Calendar;
public class TimeClock {
private int hh = 0;
private int mm = 0;
private int ss = 0;
private boolean stop = false;
private final long max = 24 * 60 * 60 + 60 * 60 + 59;
public void setNow() {// 取得現在時間
Calendar now = Calendar.getInstance();// 產生Calendar的物件,將時間放入
this.hh = now.get(Calendar.HOUR_OF_DAY);// 取得小時
this.mm = now.get(Calendar.MINUTE);// 取得分鐘
this.ss = now.get(Calendar.SECOND);// 取得秒鐘
}
private long getLong() {
return this.hh * 3600 + this.mm * 60 + this.ss;
}
private void setLong(long value) {
this.hh = (int) value / 3600;
this.mm = (int) (value - this.hh * 3600) / 60;
this.ss = (int) (value - this.hh * 3600) % 60;
// System.out.println(this.hh +":"+this.mm+":"+this.ss);
}
public void setTime(int h, int m, int s) {
this.hh = h;
this.mm = m;
this.ss = s;
}
public String getTime() {
return String.format("%2d:%02d:%02d", this.hh, this.mm, this.ss);
}
public boolean isStop() {
return stop;
}
public void Stop() {
this.stop = true;
}
public void Start() {
this.stop = false;
}
public void toDown() {
if (this.stop)
return;
long t = getLong();
if (t > 0)// 防止小於0
setLong(t - 1);
else
stop = true;
}
public void toUp() {
if (this.stop)
return;
long t = getLong();
if (t < max)// 防止超過一天
setLong(t + 1);
else
stop = true;
}
public static void main(String args[]) {
TimeClock time = null;
for (int i = 0; i < 3; i++) {
if (time == null) {
time = new TimeClock();
time.setNow();// 取得現在時間
} else {
time.toUp();
}
// System.out.println(time.getTime());
}
}
}
package test.clock;
public class NowTimeThread extends Thread {
private Clock c = null;
private TimeClock time = null;
public NowTimeThread(Clock c) {
this.c = c;
}
public void run() {
while (this.time == null || this.time.isStop() == false) {
if (this.time == null) {
this.time = new TimeClock();
this.time.setNow();// 取得現在時間
} else {
this.time.toUp();
}
c.setTime(1, this.time.getTime());
try {
sleep(1000);
} catch (java.lang.InterruptedException e) {
e.printStackTrace();
}
}
}
}
package test.clock;
public class UpTimeThread implements java.lang.Runnable {
private Clock c = null;
private TimeClock time = null;
public UpTimeThread() {
}
public UpTimeThread(Clock c) {
init(c);
}
public void init(Clock c) {
this.c = c;
this.time = new TimeClock();
setStop();
}
public void run() {
while (this.time.isStop() == false) {
this.time.toUp();
c.setTime(2, this.time.getTime());
try {
Thread.sleep(1000);
} catch (java.lang.InterruptedException e) {
e.printStackTrace();
}
}
}
public void setStop() {
this.time.Stop();
}
public void setStart() {
this.time.Start();
}
public void setTime(int h, int m, int s) {
this.time.setTime(h, m, s);
}
public void setClear()
{
this.c.setTime(2, "");
}
}
package test.clock;
public class DownTimeThread implements java.lang.Runnable {
private Clock c = null;
private TimeClock time = null;
public DownTimeThread() {
}
public DownTimeThread(Clock c) {
init(c);
}
public void init(Clock c) {
this.c = c;
this.time = new TimeClock();
setStop();
}
public void run() {
while (this.time.isStop() == false) {
this.time.toDown();
c.setTime(3, this.time.getTime());
try {
Thread.sleep(1000);
} catch (java.lang.InterruptedException e) {
e.printStackTrace();
}
}
}
public void setStop() {
this.time.Stop();
}
public void setStart() {
this.time.Start();
}
public void setTime(int h, int m, int s) {
this.time.setTime(h, m, s);
}
}
參考資料
請問鬧鐘該如何寫
應該要有一個thread檢查你設定要鬧鐘的時間(每秒check一次),當一樣時撥放音樂,撥完後離開這個thread
大概的概念應該這樣子吧
抱歉可否再詳細一點
我用NowTimeThread這個thread下去改一下給你看
在run裡面改成
if (this.time == null) { this.time = new TimeClock(); this.time.setNow();// 取得現在時間 } else { this.time.toUp(); } if(this.time.getH()==h && this.time.getM()==m && this.time.getS()==s) { //new 一個thread 去撥鬧鐘音樂 } c.setTime(1, this.time.getTime());記得TimeClock要加三個function 來取得時分秒
^^ 你好,請問可以下載你做好的jar檔嗎?
但我找不到下載連結 ^^||
我把路徑加上去了^^
你可以下載看看嚕,有問題再跟我講
^^ 感謝你無私的分享
我已經下載了,真是謝謝啊!