本文目錄一覽:
JAVA計時器,怎麼寫
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TimeCount extends JFrame implements ActionListener{
ThreadCount tc=new ThreadCount(this);
Thread thread=new Thread(tc);
JPanel panelN=new JPanel(),panelC=new JPanel();
JLabel label=new JLabel(“計時器”);
JButton butnStart=new JButton(“開始”);
boolean toEnd;
public TimeCount() {
setBounds(100,100,300,300);
setVisible(true);
label.setFont(new Font(null,Font.BOLD,22));
panelN.add(label);
add(panelN,BorderLayout.NORTH);
panelC.add(butnStart);
add(panelC,BorderLayout.CENTER);
butnStart.addActionListener(this);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==butnStart){
if(!thread.isAlive()){
thread=new Thread(tc);
thread.start();
}else {
toEnd=true;
}
}
}
public static void main(String[] args) {
new TimeCount();
}
}
class ThreadCount implements Runnable{
TimeCount lc;
public ThreadCount(TimeCount lc) {
super();
this.lc = lc;
}
public void run() {
int i=1;
while(true){
if(lc.toEnd){
lc.toEnd=false;
lc.butnStart.setText(“開始”);
return;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO: handle exception
}
i+=2;
int min=i/60000;
int second=(i%60000)/1000;
int mm=i%1000;
String show=””;
if(min0)
show+=min+”:”;
if(second0)
show+=second+”.”;
show+=mm;
lc.label.setText(show);
}
}
}
滿意請採納。
如何用java實現一個計時器?
用java實現一個計時器的方法:
public class TestDingShi implements Runnable
{
Thread xc;
Dao 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定時在此
}
}
}
寫一個計時器 JAVA代碼是什麼?
應該用線程裡面的Timer來控制package com.sy.game.test;
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();
}
}
做一個Java計時器?
您好,茫茫人海之中,能為君排憂解難實屬朕的榮幸,在下拙見,若有錯誤,還望見諒!。展開全部
怎麼還沒人回答,看不過去了,用不用多線程根據你的程序需要,
import java.io.IOException;
import java.util.Timer;
public class TimerTest {
public static void main(String[] args){
Timer timer = new Timer();
timer.schedule(new MyTask(), 1000, 2000);//在1秒後執行此任務,每次間隔2秒,如果傳遞一個Data參數,就可以在某個固定的時間執行這個任務.
while(true){//這個是用來停止此任務的,否則就一直循環執行此任務了
try {
int ch = System.in.read();
if(ch-‘c’==0){
timer.cancel();//使用這個方法退出任務
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static class MyTask extends java.util.TimerTask{
@Override
public void run() {
//你要進行的操作
}
}
}
大概就是這樣了,在根據你的業務需要查一下資料,就可以搞定了!非常感謝您的耐心觀看,如有幫助請採納,祝生活愉快!謝謝!
用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(); } } } }}
用JAVA編寫計時器
計時器可以使用timer類也可以使用線程類來操作,下面是Thread做的簡單的計時器
public class Calculagraph extends Thread {
public static void main(String[] args) {
new Calculagraph().start();
}
private long now = 0l;
private long start = System.currentTimeMillis();// 程序啟動時間的毫秒值
private long time;
public void run() {
while (true) {
now = System.currentTimeMillis();// 獲取一秒之後的毫秒值
time = now – start;// 兩個時間相減的到毫秒差
System.out.format(“%02d:%02d:%02d\n”,
time / (1000 * 60 * 60) % 60/* 時 */,
time / (1000 * 60)% 60/* 分 */,
time / 1000 % 60/* 秒 */);// 格式化字元串輸出
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
原創文章,作者:RGCUR,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/330269.html