樓主,依題意,完整的程序如下:
我們是從2013年開(kāi)始的成都網(wǎng)站建設(shè)公司,提供網(wǎng)站建設(shè),電商網(wǎng)站設(shè)計(jì)開(kāi)發(fā),成都外貿(mào)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì),微信小程序定制開(kāi)發(fā)、等服務(wù)。為客戶創(chuàng)造有價(jià)值的品牌營(yíng)銷體驗(yàn),讓互聯(lián)網(wǎng)提升企業(yè)的競(jìng)爭(zhēng)力!
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Exam2 extends Thread implements ActionListener
{
//聲明界面元素
private JFrame timeFrame;
private JButton startButton;
private JButton stopButton;
private JButton resetButton;
private JLabel timeLabel;
//定義變量存儲(chǔ)時(shí)、分、秒
int hour = 0;
int minute = 0;
int second = 0;
//聲明線程對(duì)象
Thread myThread;
public Exam2()
{
timeFrame = new JFrame("計(jì)時(shí)器");
startButton = new JButton("開(kāi)始");
stopButton = new JButton("停止");
resetButton = new JButton("重置");
timeLabel = new JLabel("00:00:00");
timeFrame.setLayout(new FlowLayout());
timeFrame.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 300)/2,
(Toolkit.getDefaultToolkit().getScreenSize().height - 200)/2, 300, 200);
timeFrame.add(timeLabel);
timeFrame.add(startButton);
timeFrame.add(stopButton);
timeFrame.add(resetButton);
timeFrame.setVisible(true);
timeFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
startButton.addActionListener(this);
stopButton.addActionListener(this);
resetButton.addActionListener(this);
myThread = this;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == this.startButton)
{
System.out.println("計(jì)時(shí)開(kāi)始...");
if(this.start)
{
this.myThread.start();
}
else
{
this.start = true;
}
}
if(e.getSource() == this.stopButton)
{
System.out.println("計(jì)時(shí)停止...");
this.start = false;
}
if(e.getSource() == this.resetButton)
{
hour = 0;
minute = 0;
second = 0;
}
}
volatile boolean start = true;
@Override
public void run()
{
while(true)
{
if(this.start)
{
second++;
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
if(second = 60)
{
second = 0;
minute++;
}
if(minute = 60)
{
minute = 0;
hour++;
}
showTime();
}
}
}
private void showTime()
{
String strTime = "";
if(hour 10)
{
strTime += "0"+hour + ":";
}
else
{
strTime += hour + ":";
}
if(minute 10)
{
strTime += "0"+minute + ":";
}
else
{
strTime += minute + ":";
}
if(second 10)
{
strTime += "0"+second;
}
else
{
strTime += second;
}
this.timeLabel.setText(strTime);
}
public static void main(String[] args)
{
new Exam2();
}
}
有問(wèn)題歡迎提問(wèn),滿意請(qǐng)采納,謝謝!
java自動(dòng)補(bǔ)全按的是“Alt + /”。
為了方便,可以設(shè)置自動(dòng)聯(lián)想。
window-preference,然后搜索assist,然后在相應(yīng)的位置輸入全部字符,而且也可以修改延遲時(shí)間,ms級(jí)別。然后apply,ok。
之后輸入幾個(gè)字母后,會(huì)自動(dòng)彈出提示
eclipse菜單欄windows---preferences----左側(cè)展開(kāi)General----key---找到Content?Assist選中??在下面的快捷鍵位置把原來(lái)的刪除?添加新的快捷鍵就可以了,我的是alt+j??這樣收可以不用離開(kāi)鍵盤?可以參考一下
Windows——Preferences——Java--Editor--Content Asist,在Auto activation triggers for Java后面的文本框里填寫“.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”就行,默認(rèn)為"."