應該用線程里面的Timer來控制package com.sy.game.test;
為岳麓等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及岳麓網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為網(wǎng)站制作、成都網(wǎng)站設計、岳麓網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
import java.util.Timer;
import java.util.TimerTask;
public class TimeTask {
public static void main(String[] args) {
TimeTask tTask=new TimeTask();
tTask.timeVoid();
}
public void timeVoid(){
final Timer timer = new Timer();
TimerTask tt=new TimerTask() {
@Override
public void run() {
System.out.println("到點啦!");
timer.cancel();
}
};
timer.schedule(tt, 3000);
}
}
整合的:
/*
* java倒計時器
* shiyang
* */
package com.sy.game.test;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
@SuppressWarnings("unused")
public class TimeController extends JFrame implements ActionListener {
private static final long serialVersionUID = 4603262282860990473L;
private static final int DEFAULT_WIDTH = 200;
private static final int DEFAULT_HEIGHT = 100;
private static final int width = Toolkit.getDefaultToolkit()
.getScreenSize().width;
private static final int height = Toolkit.getDefaultToolkit()
.getScreenSize().height;
private Container container;
private JButton btn;
private JTextField jtfTime;
private Timer tmr;
public TimeController() {
initComponents();
Timer tmr = new Timer(1000, this);
this.tmr = tmr;
setVisible(true);
}
private void initComponents() {
this.setTitle("SY秒表");
this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
this.setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation((width - DEFAULT_WIDTH) / 2,
(height - DEFAULT_HEIGHT) / 2);
jtfTime = new JTextField("10");
btn = new JButton("開始倒計時");
container = getContentPane();
JPanel panel = new JPanel();
panel.add(btn);
panel.add(jtfTime);
this.add(panel);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btn) {
jtfTime.setText("10");
tmr.start();
} else {
int t;
t = Integer.parseInt(jtfTime.getText());
t--;
jtfTime.setText("" + t);
if (t = 0) {
tmr.stop();
}
}
}
public static void main(String[] args) {
TimeController timeController = new TimeController();
}
}
import?java.awt.BorderLayout;
import?java.awt.Container;
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.awt.event.WindowEvent;
import?java.awt.event.WindowListener;
import?java.io.*;
import?java.util.*;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JOptionPane;
import?javax.swing.JPanel;
import?javax.swing.JTextField;
import?javax.swing.plaf.OptionPaneUI;
public?class?Demo?{
static?boolean?isRuning=false;
static?boolean?isFirst=true;
@SuppressWarnings("unchecked")
public?static?void?main(String[]?args)?throws?Exception?{
JFrame?form1?=?new?JFrame("Form1");
JTextField?jTextField?=?new?JTextField(10);
jTextField.setSize(10,?10);
jTextField.setText("0");
jTextField.setEditable(false);
JButton?jButton?=?new?JButton("開始");
jButton.setSize(10,?10);
Thread?thread?=?new?Thread(new?Runnable()?{
@Override
public?void?run()?{
while?(true)?{
while(isRuning){
Integer?counter?=?Integer.parseInt(jTextField.getText().trim());
counter++;
jTextField.setText(counter.toString());
try?{
Thread.sleep(1000);
}?catch?(Exception?e2)?{
}
}
}
}
});
jButton.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
???String?text=jButton.getText().equals("開始")?"暫停":"開始";
???jButton.setText(text);
???isRuning=!isRuning;
???if(isFirst){
???thread.start();
???isFirst=false;
???}
}
});
JPanel?panel?=?new?JPanel();
panel.setSize(200,?200);
panel.add(jTextField,?BorderLayout.NORTH);
panel.add(jButton,?BorderLayout.CENTER);
form1.add(panel);
form1.setBounds(200,?100,?250,?150);
form1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form1.addWindowListener(new?WindowListener()?{
@Override
public?void?windowOpened(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowIconified(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowDeiconified(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowDeactivated(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowClosing(WindowEvent?e)?{
//?窗口關閉前取出文本框的數(shù)字保存到外部文件,代碼在此處寫
JOptionPane.showMessageDialog(null,?"Are?you?sure?closing?");
}
@Override
public?void?windowClosed(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowActivated(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
});
form1.setVisible(true);
}
}
好吧,已看到你的評論,我在這里再回答一次:
1)你所說的置頂如果是屬于懸浮窗效果,那么JFrame實例化后,再添加一行如下的代碼:
form1.setAlwaysOnTop(true);//?總是允許窗口置頂
2)時分秒更簡單了,除一除轉轉換就行了,沒有技術含量。
3)快捷鍵通過JButton類的setMnemonic方法實現(xiàn)
So,綜上,整個程序的實現(xiàn)算法如下:
package?hky.example;
import?java.awt.BorderLayout;
import?java.awt.Container;
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.awt.event.KeyEvent;
import?java.awt.event.WindowEvent;
import?java.awt.event.WindowListener;
import?java.io.*;
import?java.util.*;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JOptionPane;
import?javax.swing.JPanel;
import?javax.swing.JTextField;
import?javax.swing.plaf.OptionPaneUI;
public?class?Demo{
static?boolean?isRuning=false;
static?boolean?isFirst=true;
static?Integer?hmsCounter=0;
static?int?hour,minute,second;
@SuppressWarnings("unchecked")
public?static?void?main(String[]?args)?throws?Exception?{
JFrame?form1?=?new?JFrame("Form1");
form1.setAlwaysOnTop(true);//?1)總是允許窗口置頂
JTextField?jTextField?=?new?JTextField(10);
jTextField.setSize(10,?10);
jTextField.setText("0");
jTextField.setEditable(false);
JButton?jButton?=?new?JButton("開始");
jButton.setSize(10,?10);
Thread?thread?=?new?Thread(new?Runnable()?{
@Override
public?void?run()?{
while?(true)?{
while(isRuning){
++hmsCounter;?
//3)時分秒顯示
hour=hmsCounter/3600;
minute=hmsCounter%3600/60;
second=hmsCounter%60;
jTextField.setText(hour+"時"+minute+"分"+second+"秒");
try?{Thread.sleep(1000);}?catch?(Exception?e2)?{}
}
try?{Thread.sleep(200);}?catch?(Exception?e2)?{}//?修復上一次回答的版本可能會存在的Bug
}
}
});
jButton.setMnemonic(KeyEvent.VK_ENTER);//?2)給JButton發(fā)送?Alt+Enter快捷鍵
jButton.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
String?text=jButton.getText().equals("開始")?"暫停":"開始";
jButton.setText(text);
isRuning=!isRuning;
if(isFirst){
thread.start();
isFirst=false;
}
}
});
JPanel?panel?=?new?JPanel();
panel.setSize(200,?200);
panel.add(jTextField,?BorderLayout.NORTH);
panel.add(jButton,?BorderLayout.CENTER);
form1.add(panel);
form1.setBounds(200,?100,?250,?150);
form1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form1.addWindowListener(new?WindowListener()?{
@Override
public?void?windowOpened(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowIconified(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowDeiconified(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowDeactivated(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowClosing(WindowEvent?e)?{
//?窗口關閉前取出文本框的數(shù)字保存到外部文件,代碼在此處寫
JOptionPane.showMessageDialog(null,?"Are?you?sure?closing?");
}
@Override
public?void?windowClosed(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowActivated(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
});
form1.setVisible(true);
}
}