用Java編計(jì)時(shí)器代碼,java 計(jì)時(shí)函數(shù)

求人用java編寫(xiě)一條計(jì)時(shí)器代碼。

import?java.awt.BorderLayout;

輝縣網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,輝縣網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為輝縣數(shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的輝縣做網(wǎng)站的公司定做!

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("開(kāi)始");

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("開(kāi)始")?"暫停":"開(kāi)始";

???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)?{

//?窗口關(guān)閉前取出文本框的數(shù)字保存到外部文件,代碼在此處寫(xiě)

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);

}

}

JAVA計(jì)時(shí)器的JAVA代碼

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class Clock extends Applet {

private final Panel pnlTop = new Panel();

private final Panel pnlBot = new Panel();

private final Label lblDate = new Label();

private final Label lblTime = new Label();

private final Label lblWatch = new Label();

private final Button btnGo = new Button("開(kāi)始");

private final Button btnReset = new Button("重置");

private final Label lblSplit = new Label();

private final Button btnSplit = new Button("定點(diǎn)");

private final Button btnSplitReset = new Button("定點(diǎn)重置");

private final Button btnLapAdd = new Button("沖線");

private final Button btnLapReset = new Button("沖線重置");

private final java.awt.List lstLaps = new java.awt.List();

private final UpdateClockThread ucThread = new UpdateClockThread();

private final StopwatchThread swThread = new StopwatchThread();

private class btnGoListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

if ((btnGo.getLabel().equals("開(kāi)始")) ||

(btnGo.getLabel().equals("繼續(xù)"))) {

// Start the clock!

swThread.go();

btnGo.setLabel("停止");

btnGo.setBackground(Color.red);

} else if (btnGo.getLabel().equals("停止")) {

// Stop the clock!

swThread.noGo();

btnGo.setLabel("繼續(xù)");

btnGo.setBackground(Color.green);

}

}

}

private class btnResetListener implements ActionListener {

/** Actually run when the button gets clicked.

*@param e The event

*/

public void actionPerformed(ActionEvent e) {

swThread.reset();

btnGo.setLabel("開(kāi)始");

btnGo.setBackground(Color.green);

}

}

/** Listens to the Split button.

* @version CS2136 - Term D'00 - Assignment 5

* @author Peter Cooper Jr.

*/

private class btnSplitListener implements ActionListener {

/** Actually run when the button gets clicked.

*@param e The event

*/

public void actionPerformed(ActionEvent e) {

lblSplit.setText(lblWatch.getText());

}

}

/** Listens to the Split Reset button.

* @version CS2136 - Term D'00 - Assignment 5

* @author Peter Cooper Jr.

*/

private class btnSplitResetListener implements ActionListener {

/** Actually run when the button gets clicked.

*@param e The event

*/

public void actionPerformed(ActionEvent e) {

lblSplit.setText("");

}

}

/** Listens to the Lap Add button.

* @version CS2136 - Term D'00 - Assignment 5

* @author Peter Cooper Jr.

*/

private class btnLapAddListener implements ActionListener {

/** Actually run when the button gets clicked.

*@param e The event

*/

public void actionPerformed(ActionEvent e) {

swThread.addLap();

}

}

/** Listens to the Lap Reset button.

* @version CS2136 - Term D'00 - Assignment 5

* @author Peter Cooper Jr.

*/

private class btnLapResetListener implements ActionListener {

/** Actually run when the button gets clicked.

*@param e The event

*/

public void actionPerformed(ActionEvent e) {

swThread.resetLap();

}

}

/** A thread that updates the current date time.

* @version CS2136 - Term D'00 - Assignment 5

* @author Peter Cooper Jr.

*/

private class UpdateClockThread extends Thread {

/** The actual work of the thread.

*/

public void run() {

while (true) {

Calendar now = Calendar.getInstance();

String month = Integer.toString(now.get(Calendar.MONTH)+1);

String date = Integer.toString(now.get(Calendar.DAY_OF_MONTH));

String year = Integer.toString(now.get(Calendar.YEAR));

String hour = Integer.toString(now.get(Calendar.HOUR));

if (hour.equals("0")) hour = "12";

String minute = Integer.toString(now.get(Calendar.MINUTE));

if (minute.length() == 1) minute = "0" + minute;

String second = Integer.toString(now.get(Calendar.SECOND));

if (second.length() == 1) second = "0" + second;

String ampm = now.get(Calendar.AM_PM) == Calendar.AM

? "AM" : "PM";

lblDate.setText(month + "/" + date + "/" + year);

lblTime.setText(hour + ":" + minute + ":" + second

+ " " + ampm);

try {

sleep(500);

} catch (InterruptedException e) {}

}

}

}

private class StopwatchThread extends Thread {

/** Whether or not stopwatch is running. */

private boolean going = false;

/** Stores elapsed milliseconds of previous runs. */

private long prevElapsed = 0;

/** Stores beginning time of this run. */

private Date startDate = new Date();

/** Current lap number. */

private int lapNum = 0;

/** Elapsed time at end of last lap. */

private long lastLapTime = 0;

/** Returns elapsed time in milliseconds.

*@return The elapsed time

*/

private long elapsedTime() {

return prevElapsed +

(going ? new Date().getTime() - startDate.getTime() : 0);

}

/** Changes the number of elapsed milliseconds into a string.

*@param time Number of elapsed milliseconds

*@return The elapsed time as a string.

*/

private String msToString(long time) {

String ms, sec, min;

if (time % 10 = 5) //round to nearest hundredth

time += 5;

ms = Long.toString(time % 1000);

while (ms.length() 3)

ms = "0" + ms;

ms = ms.substring(0, ms.length() - 1);

time /= 1000;

sec = Long.toString(time % 60);

if (sec.length() == 1) sec = "0" + sec;

time /= 60;

min = Long.toString(time);

return min + ":" + sec + "." + ms;

}

public void go() {

startDate = new Date();

going = true;

}

public void noGo() {

prevElapsed = elapsedTime();

going = false;

}

public void reset() {

going = false;

prevElapsed = 0;

lastLapTime = 0;

}

public void addLap() {

long elapsed = elapsedTime();

lstLaps.add("沖線 " + Integer.toString(++lapNum)+ " -- " +

"用時(shí): " + msToString(elapsed) + " -- " +

"沖線時(shí)間: " + msToString(elapsed - lastLapTime));

lastLapTime = elapsed;

}

/** Resets the lap list.

*/

public void resetLap() {

lstLaps.removeAll();

lapNum = 0;

lastLapTime = 0;

}

/** Main code of the thread.

*/

public void run() {

while (true) {

lblWatch.setText(msToString(elapsedTime()));

yield();

}

}

}

public void init() {

setLayout(new GridLayout(2,1));

setBackground(Color.lightGray);

setForeground(Color.black);

pnlTop.setLayout(new GridLayout(4,4));

pnlTop.add(new Label("日期:"));

pnlTop.add(lblDate);

pnlTop.add(new Label("時(shí)間:"));

pnlTop.add(lblTime);

pnlTop.add(new Label("計(jì)時(shí):"));

//lblWatch.setBackground(Color.black);

lblWatch.setForeground(Color.blue);

pnlTop.add(lblWatch);

pnlTop.add(btnGo);

btnGo.setBackground(Color.green);

pnlTop.add(btnReset);

pnlTop.add(new Label("定點(diǎn):"));

pnlTop.add(lblSplit);

pnlTop.add(btnSplit);

pnlTop.add(btnSplitReset);

pnlTop.add(new Label("沖線時(shí)間:"));

pnlTop.add(new Label());

pnlTop.add(btnLapAdd);

pnlTop.add(btnLapReset);

pnlBot.setLayout(new GridLayout(1,1));

pnlBot.add(lstLaps);

add(pnlTop);

add(pnlBot);

btnGo.addActionListener(new btnGoListener());

btnReset.addActionListener(new btnResetListener());

btnSplit.addActionListener(new btnSplitListener());

btnSplitReset.addActionListener(new btnSplitResetListener());

btnLapAdd.addActionListener(new btnLapAddListener());

btnLapReset.addActionListener(new btnLapResetListener());

swThread.setDaemon(true);

ucThread.setDaemon(true);

swThread.start();

ucThread.start();

}

public static void main(String[] args) {

Clock applet = new Clock();

Frame aFrame = new Frame("計(jì)時(shí)器");

aFrame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

aFrame.add(applet, BorderLayout.CENTER);

aFrame.setSize(400, 200);

applet.init();

applet.start();

aFrame.setVisible(true);

}

}

做一個(gè)Java計(jì)時(shí)器?

您好,茫茫人海之中,能為君排憂解難實(shí)屬朕的榮幸,在下拙見(jiàn),若有錯(cuò)誤,還望見(jiàn)諒!。展開(kāi)全部

怎么還沒(méi)人回答,看不過(guò)去了,用不用多線程根據(jù)你的程序需要,

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秒后執(zhí)行此任務(wù),每次間隔2秒,如果傳遞一個(gè)Data參數(shù),就可以在某個(gè)固定的時(shí)間執(zhí)行這個(gè)任務(wù).

while(true){//這個(gè)是用來(lái)停止此任務(wù)的,否則就一直循環(huán)執(zhí)行此任務(wù)了

try {

int ch = System.in.read();

if(ch-'c'==0){

timer.cancel();//使用這個(gè)方法退出任務(wù)

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

static class MyTask extends java.util.TimerTask{

@Override

public void run() {

//你要進(jìn)行的操作

}

}

}

大概就是這樣了,在根據(jù)你的業(yè)務(wù)需要查一下資料,就可以搞定了!非常感謝您的耐心觀看,如有幫助請(qǐng)采納,祝生活愉快!謝謝!

JAVA計(jì)時(shí)器,怎么寫(xiě)

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("計(jì)時(shí)器");

JButton butnStart=new JButton("開(kāi)始");

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("開(kāi)始");

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);

}

}

}

滿(mǎn)意請(qǐng)采納。

寫(xiě)一個(gè)計(jì)時(shí)器 JAVA代碼是什么?

應(yīng)該用線程里面的Timer來(lái)控制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("到點(diǎn)啦!");

timer.cancel();

}

};

timer.schedule(tt, 3000);

}

}

整合的:

/*

* java倒計(jì)時(shí)器

* 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("開(kāi)始倒計(jì)時(shí)");

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編寫(xiě)一個(gè)倒計(jì)時(shí)器代碼。

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("開(kāi)始"); private JButton reset = new JButton("重置"); private JPanel panel = new JPanel(); private boolean isRunning; private int time; private int timeBetween; public TimerDemo(int timeBetween) { super("計(jì)時(shí)器"); this.timeBetween = timeBetween; try { init(); } catch (Exception e) { e.printStackTrace(); } } public TimerDemo() { super("計(jì)時(shí)器"); 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);// 設(shè)定 1ms/次 // new TimerDemo(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == start) { if (start.getText().equals("開(kāi)始")) { start.setText("暫停"); isRunning = true; } else if (start.getText().equals("暫停")) { start.setText("開(kāi)始"); isRunning = false; } } if (e.getSource() == reset) { start.setText("開(kāi)始"); 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(); } } } }}

本文名稱(chēng):用Java編計(jì)時(shí)器代碼,java 計(jì)時(shí)函數(shù)
轉(zhuǎn)載注明:http://muchs.cn/article14/hcjdge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、App開(kāi)發(fā)軟件開(kāi)發(fā)、域名注冊(cè)網(wǎng)站改版、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)