第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

計時器問題

計時器問題

橙子lovesun 2015-12-24 15:28:55
用Java編寫的計時器,有三按鈕。開始,停止,復(fù)位。問題出在:按下復(fù)位后再按開始,還是從按下停止的時間開始計時。請問下應(yīng)該怎么改寫代碼,使得按下開始就是從0計時。代碼如下: import?java.awt.Button; import?java.awt.Color; import?java.awt.FlowLayout; import?java.awt.PopupMenu; import?java.awt.TextField; import?java.awt.event.ActionEvent; import?java.awt.event.ActionListener; import?java.text.SimpleDateFormat; import?java.util.Calendar; import?java.util.Date; import?javax.swing.JFrame; import?javax.swing.JLabel; import?javax.swing.Timer; public?class?TimeCount?extends?JFrame?{ JLabel?lbl?=?new?JLabel(); Date?now?=?new?Date(); public?TimeCount()?{ now.setHours(0); now.setMinutes(0); now.setSeconds(0); final?Timer?timer?=?new?Timer(1000,?new?ActionListener()?{ public?void?actionPerformed(ActionEvent?e)?{ Date?now2?=?new?Date(now.getTime()?+?1000); now?=?now2; SimpleDateFormat?formatter?=?new?SimpleDateFormat("HH:mm:ss"); lbl.setText(formatter.format(now)); } }); Button?b1?=?new?Button("開始計時"); Button?b2?=?new?Button("停止計時"); Button?b3?=?new?Button("復(fù)位"); b1.setSize(100,?50); b2.setSize(100,?50); b3.setSize(100,?50); b1.addActionListener(new?ActionListener()?{ @Override public?void?actionPerformed(ActionEvent?e)?{ Button?b?=?(Button)?e.getSource(); b.setLabel("開始計時"); timer.start(); } }); b2.addActionListener(new?ActionListener()?{ @Override public?void?actionPerformed(ActionEvent?e)?{ Button?b?=?(Button)?e.getSource(); b.setLabel("停止計時"); timer.stop(); } }); b3.addActionListener(new?ActionListener()?{ @Override public?void?actionPerformed(ActionEvent?e)?{ lbl.setText("00:00:00"); } }); this.setLayout(new?FlowLayout()); //this.add(txt); this.add(b1); this.add(b2); this.add(b3); this.add(lbl); this.setSize(300,?200); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } public?static?void?main(String[]?args)?{ TimeCount?t?=?new?TimeCount(); } }
查看完整描述

3 回答

已采納
?
管理員

TA貢獻(xiàn)11條經(jīng)驗(yàn) 獲得超2個贊

try?{
	now?=?formatter.parse("00:00:00");
}?catch?(ParseException?e1)?{
	e1.printStackTrace();
}
lbl.setText("00:00:00");


查看完整回答
反對 回復(fù) 2015-12-25
?
努力提升

TA貢獻(xiàn)65條經(jīng)驗(yàn) 獲得超15個贊

你應(yīng)該把時間重置為0時0分0秒,不是光把label的文本內(nèi)容都改成0啊,你這樣對計時器根本沒影響啊。

查看完整回答
反對 回復(fù) 2015-12-24
?
Mr_atom

TA貢獻(xiàn)8條經(jīng)驗(yàn) 獲得超5個贊

開始前要初始化時間,或者在停止的時候初始化。

查看完整回答
反對 回復(fù) 2015-12-24
  • 3 回答
  • 0 關(guān)注
  • 1361 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號