1. 如何用java實現一個計時器
用java實現一個計時器的方法:
public class TestDingShi implements Runnable
{
Thread xc;
Dao =new DaoImpl();
public TestDingShi()
{
xc=new Thread(this);//線程開啟
xc.start();
}
public void run()
{
while (true)
{
try
{
xc.sleep(1000);//睡眠開始計時
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//TODO定時在此
}
}
}
2. 用JAVA編寫計時器
計時器可以使用timer類也可以使用線程類來操作,下面是Thread做的簡單的計時器
{
publicstaticvoidmain(String[]args){
newCalculagraph().start();
}
privatelongnow=0l;
privatelongstart=System.currentTimeMillis();//程序啟動時間的毫秒值
privatelongtime;
publicvoidrun(){
while(true){
now=System.currentTimeMillis();//獲取一秒之後的毫秒值
time=now-start;//兩個時間相減的到毫秒差
System.out.format("%02d:%02d:%02d
",
time/(1000*60*60)%60/*時*/,
time/(1000*60)%60/*分*/,
time/1000%60/*秒*/);//格式化字元串輸出
try{
Thread.sleep(1000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}
3. java 倒計時的程序
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TimeThreadFrame extends JFrame{
// 定義組件
private JLabel lblTime;
private JTextField txtInput;
private JButton btnEnter;
// 構造方法
public TimeThreadFrame(){
// 設置窗體的相關屬性
super("TimerThread");
this.setSize(300,200);
this.setLayout(null);
this.setLocation(100,50);
// 創建組件
this.lblTime = new JLabel("請輸入倒計時時間");
this.lblTime.setBounds(30,20,200,30);
this.txtInput = new JTextField();
this.txtInput.setBounds(30,70,100,30);
this.btnEnter = new JButton("確定");
this.btnEnter.setBounds(150,70,70,30);
// 給JTextField注冊監聽
this.txtInput.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent ke) {
}
public void keyReleased(KeyEvent ke) {
}
public void keyTyped(KeyEvent ke) {
txtInput_KeyTyped(ke);
}
});
// 給JButton注冊監聽
this.btnEnter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
btnEnter_ActionPerformed(ae);
}
});
// 將各組件添加到窗體上
add(lblTime);
add(txtInput);
add(btnEnter);
// 顯示窗體
this.setVisible(true);
}
// 輸入時的事件處理,控制用戶只能輸入數字
public void txtInput_KeyTyped(KeyEvent ke){
if(ke.getKeyChar() < '0' || ke.getKeyChar() > '9'){
ke.setKeyChar('\0');
}
}
// 點擊按鈕時的事件處理,核心!
public void btnEnter_ActionPerformed(ActionEvent ae){
// 獲得用戶輸入的倒計時時間
String strTime = this.txtInput.getText();
if(strTime.equals("")){
// 檢測用戶是否輸入
this.lblTime.setText("您尚未輸入,請輸入!");
}
else{
Integer time = Integer.parseInt(strTime);
// 創建線程
TimeThread tt = new TimeThread(this.lblTime,time);
tt.start();
// 創建Timer
Timer timer = new Timer();
timer.schele(new TimerTask(){
// 啟動其他程序
public void run() {
System.out.print("ok");
}
}, time * 1000);
}
}
// 啟動窗體
public static void main(String[] args){
new TimeThreadFrame();
}
}
// 時間線程類
class TimeThread extends Thread{
private JLabel lblTime;
private int time;
// 構造方法傳入,顯示事件的JLabel和倒計時的時間。
public TimeThread(JLabel lblTime, int time){
this.lblTime = lblTime;
this.time = time;
}
// run方法
public void run(){
while(time > 0){
// 顯示所剩時間
this.lblTime.setText("所剩時間:" + time);
// 所剩時間減少
time--;
try {
this.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
滿意請採納。
4. 用java編寫一個倒計時器代碼。
import java.awt.BorderLayout;import java.awt.Container;import java.awt.Font;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.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class TimerDemo extends JFrame implements ActionListener { private static final long serialVersionUID = 201306211111L; private JTextField screen = new JTextField("0"); private JButton start = new JButton("開始"); private JButton reset = new JButton("重置"); private JPanel panel = new JPanel(); private boolean isRunning; private int time; private int timeBetween; public TimerDemo(int timeBetween) { super("計時器"); this.timeBetween = timeBetween; try { init(); } catch (Exception e) { e.printStackTrace(); } } public TimerDemo() { super("計時器"); this.timeBetween = 100; try { init(); } catch (Exception e) { e.printStackTrace(); } } private void init() { panel.setLayout(new GridLayout()); panel.add(start); panel.add(reset); start.addActionListener(this); reset.addActionListener(this); screen.setFont(new Font("幼圓", Font.BOLD, 60)); screen.setHorizontalAlignment(JTextField.CENTER); screen.setEditable(false); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(panel, BorderLayout.SOUTH); c.add(screen, BorderLayout.CENTER); this.setSize(200, 150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { new TimerDemo(1);// 設定 1ms/次 // new TimerDemo(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == start) { if (start.getText().equals("開始")) { start.setText("暫停"); isRunning = true; } else if (start.getText().equals("暫停")) { start.setText("開始"); isRunning = false; } } if (e.getSource() == reset) { start.setText("開始"); screen.setText("0"); isRunning = false; time = 0; } new Thread(new TimeZone()).start(); } class TimeZone implements Runnable { @Override public void run() { while (isRunning) { time++; if (time >= Integer.MAX_VALUE) { screen.setText("ERROR"); JOptionPane.showMessageDialog(null, "ERROR"); isRunning = false; } screen.setText(String.valueOf(time)); try { Thread.sleep(timeBetween); } catch (Exception e) { e.printStackTrace(); } } } }}
5. java中如何實現自動計時功能,就是點擊一個start按鈕就開始計時,以秒為單位
簡單代碼如下:
importjava.awt.Button;
importjava.awt.FlowLayout;
importjava.awt.Label;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjavax.swing.JFrame;
importjavax.swing.Timer;
@SuppressWarnings("serial")
{
finalLabellab=newLabel();
Datenow=newDate();
@SuppressWarnings("deprecation")
publicTimers(){
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
setBounds(550,270,200,150);
finalTimertimer=newTimer(1000,newActionListener(){
publicvoidactionPerformed(ActionEvente){
Datenow2=newDate(now.getTime()+1000);
now=now2;
SimpleDateFormatformatter=newSimpleDateFormat("HH:mm:ss");
lab.setText(formatter.format(now));
}
});
Buttonb1=newButton("開始");
Buttonb2=newButton("停止");
b2.setBounds(40,40,40,40);
b1.setBounds(30,30,30,30);
b1.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
Buttonb=(Button)e.getSource();
b.setLabel("開始");
timer.start();
}
});
b2.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
Buttonb=(Button)e.getSource();
b.setLabel("停止");
timer.stop();
}
});
this.setLayout(newFlowLayout());
this.add(b2);
this.add(b1);
this.add(lab);
this.setSize(300,200);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
Timerst=newTimers();
t.lab.setText("00:00:00");
}
}
不知是否幫到你,如滿意請採納!
6. 用JAVA做個倒計時程序
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TimeThreadFrame extends JFrame{
// 定義組件
private JLabel lblTime;
private JTextField txtInput;
private JButton btnEnter;
// 構造方法
public TimeThreadFrame(){
// 設置窗體的相關屬性
super("TimerThread");
this.setSize(300,200);
this.setLayout(null);
this.setLocation(100,50);
// 創建組件
this.lblTime = new JLabel("請輸入倒計時時間");
this.lblTime.setBounds(30,20,200,30);
this.txtInput = new JTextField();
this.txtInput.setBounds(30,70,100,30);
this.btnEnter = new JButton("確定");
this.btnEnter.setBounds(150,70,70,30);
// 給JTextField注冊監聽
this.txtInput.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent ke) {
}
public void keyReleased(KeyEvent ke) {
}
public void keyTyped(KeyEvent ke) {
txtInput_KeyTyped(ke);
}
});
// 給JButton注冊監聽
this.btnEnter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
btnEnter_ActionPerformed(ae);
}
});
// 將各組件添加到窗體上
add(lblTime);
add(txtInput);
add(btnEnter);
// 顯示窗體
this.setVisible(true);
}
// 輸入時的事件處理,控制用戶只能輸入數字
public void txtInput_KeyTyped(KeyEvent ke){
if(ke.getKeyChar() < '0' || ke.getKeyChar() > '9'){
ke.setKeyChar('\0');
}
}
// 點擊按鈕時的事件處理,核心!
public void btnEnter_ActionPerformed(ActionEvent ae){
// 獲得用戶輸入的倒計時時間
String strTime = this.txtInput.getText();
if(strTime.equals("")){
// 檢測用戶是否輸入
this.lblTime.setText("您尚未輸入,請輸入!");
}
else{
Integer time = Integer.parseInt(strTime);
// 創建線程
TimeThread tt = new TimeThread(this.lblTime,time);
tt.start();
// 創建Timer
Timer timer = new Timer();
timer.schele(new TimerTask(){
// 啟動其他程序
public void run() {
System.out.print("ok");
}
}, time * 1000);
}
}
// 啟動窗體
public static void main(String[] args){
new TimeThreadFrame();
}
}
// 時間線程類
class TimeThread extends Thread{
private JLabel lblTime;
private int time;
// 構造方法傳入,顯示事件的JLabel和倒計時的時間。
public TimeThread(JLabel lblTime, int time){
this.lblTime = lblTime;
this.time = time;
}
// run方法
public void run(){
while(time > 0){
// 顯示所剩時間
this.lblTime.setText("所剩時間:" + time);
// 所剩時間減少
time--;
try {
this.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
7. Java怎麼給方法計時
你可以在開始和結束的時候,分別記錄下當前的時間的這毫秒數。然後再減,以下是一段代碼。
public class Test{
public static void main(String[] args) {
long startMili=System.currentTimeMillis();// 當前時間對應的毫秒數
System.out.println("開始 "+startMili);
// 執行一段代碼,求一百萬次隨機值
for(int i=0;i<1000000;i++){
Math.random();
}
long endMili=System.currentTimeMillis();
System.out.println("結束 s"+endMili);
System.out.println("總耗時為:"+(endMili-startMili)+"毫秒");
}
}