求人用java编写一条计时器代码 。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) {
// 窗口关闭前取出文本框java计时时间代码的数字保存到外部文件java计时时间代码,代码在此处写
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计时器秒表源代码import java.awt.*;
public class Clock {
private int hour;
private int minute;
private int second;
public Clock(){
}
public Clock(int hrs,int min,int sec){
hour =hrs % 12;
minute = min;
second = sec;
}
void show (Graphics g,int cx,int cy,int rad){
int hourLenght = (int)(rad * 0.5);//时针的长度
int minuteLenght = (int)(rad * 0.6);//分针的长度
int secondLenght = (int)(rad * 0.9);//秒针的长度
double angle;//角度
//画出钟面
g.drawOval(cx-rad, cy - rad, rad * 2, rad * 2);
//画出时针
angle = (double)(hour*60*60minute*60second)/43200.0*2.0*Math.PI;
drawNiddle(g,Color.blue, cx, cy, hourLenght,angle);
//画分针
angle = (double)(minute*60second)/3600 * 2.0 * Math.PI;
drawNiddle(g,Color.blue, cx, cy, minuteLenght,angle);
// 画秒针
angle = (double)(second)/60*2.0*Math.PI;
drawNiddle(g,Color.blue, cx, cy, secondLenght,angle);
}
private void drawNiddle(Graphics g ,Color c,int x,int y,int len,double angle){
int ex = (int)(xlen * Math.sin(angle));
int ey = (int)(y - len * Math.cos(angle));
g.setColor(c);
g.drawLine(x,y,ex,ey);
}
}
----------------ClockTest1.java---------------------
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class ClockTest1 extends JFrame{
/**
* @param args
*/
private Clock clock ;
private Date timeNow;
public ClockTest1(){
super("时钟");
setSize(400,400);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
timeNow = new Date();
clock = new Clock(timeNow.getHours(),timeNow.getMinutes(),timeNow.getSeconds());
clock.show(g,170,150,100);
try{
//for(int i =0 ; i 10 ; i= 10)
Thread.sleep(1000);
}catch(InterruptedException e){
}
repaint();
}
public static void main(String[] args) {
// TODO 自动生成方法存根
ClockTest1 appication = newClockTest1();
appication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
求java的计时器代码 , 应该比较简单的 , 来看看吧 。package test;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Test5 extends Applet {
private final Panel pan = new Panel();
private final Label time = new Label();
private final Button btnGo = new Button("开始");
private final Button btnPouse = new Button("暂停");
private final Button btnReset = new Button("复位");
private final StopwatchThread swThread = new StopwatchThread();
private class btnGoListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
swThread.go();
btnGo.setEnabled(false);
}
}
private class btnPouseListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(btnGo.isEnabled()){
return ;
}
if (btnPouse.getLabel().equals("继续")) {
swThread.go();
btnPouse.setLabel("暂停");
} else if (btnPouse.getLabel().equals("暂停")) {
swThread.noGo();
btnPouse.setLabel("继续");
}
}
}
private class btnResetListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
swThread.reset();
btnGo.setEnabled(true);
btnGo.setLabel("开始");
btnPouse.setLabel("暂停");
}
}
private class StopwatchThread extends Thread {
private boolean going = false;
private long prevElapsed = 0;
private Date startDate = new Date();
private long elapsedTime() {
return prevElapsed
(going ? new Date().getTime() - startDate.getTime() : 0);
}
private String msToString(long time) {
System.out.println(time "" ((0*60 2)*1000 999));
if(((99*60 59)*1000 983)=time((99*60 59)*1000 999)=time){//((0*60 2)*1000 983)=time((0*60 2)*1000 999)=time
if (time % 1000990)
time= 2;
swThread.noGo();
}
String ms, sec, min;
if (time % 10 = 5)
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;
}
public void run() {
while (true) {
time.setText(msToString(elapsedTime()));
yield();
}
}
}
public void init() {
setLayout(new GridLayout(2,2));
setBackground(Color.lightGray);
setForeground(Color.black);
pan.setLayout(new GridLayout(3,2));
pan.add(new Label("计时:"));
time.setForeground(Color.blue);
pan.add(time);
pan.add(btnGo);
pan.add(btnPouse);
pan.add(btnReset);
pan.add(new Label());
add(pan);
btnGo.addActionListener(new btnGoListener());
btnReset.addActionListener(new btnResetListener());
btnPouse.addActionListener(new btnPouseListener());
swThread.setDaemon(true);
swThread.start();
}
public static void main(String[] args) {
Test5 applet = new Test5();
Frame aFrame = new Frame("计时器");
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);
}
}
可以改变有注释java计时时间代码的那个if语句里面java计时时间代码的值来判断什么时候停止
JAVA中如何使用计时函数Java计时函数currentTimeMills()
System.currentTimeMills()计时精确到毫秒级,跟计算机以1970年1月1日0时为计时起点一样,该函数方法统计的也是从1970年1月1日0时开始,到程序运行到该函数时刻的毫秒总数 。
该函数方法定义在Java系统类System中,如果想实现程序运行计时功能也很简单,只要在程序前后分别放置该函数方法,然后后减前毫秒总数 , 就能计算程序运行的耗时 。具体实现如下:
long startTime = System.currentTimeMills(); //程序开始记录时间
// 。。。。。。
long endTime= System.currentTimeMills(); //程序结束记录时间
long TotalTime = endTime - startTime;//总消耗时间
Java怎么给方法计时?你可以在开始和结束的时候,分别记录下当前的时间的这毫秒数 。然后再减,以下是一段代码 。
public class Test{
public static void main(String[] args) {
long startMili=System.currentTimeMillis();// 当前时间对应的毫秒数
System.out.println("开始 " startMili);
// 执行一段代码,求一百万次随机值
for(int i=0;i1000000;i){
Math.random();
}
long endMili=System.currentTimeMillis();
System.out.println("结束 s" endMili);
System.out.println("总耗时为:" (endMili-startMili) "毫秒");
}
}
写一个计时器 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计时时间代码 java 计时】java计时时间代码的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于java 计时、java计时时间代码的信息别忘了在本站进行查找喔 。
推荐阅读
- 360浏览器太老了不支持html5视频的简单介绍
- 网络电视密码怎么改,网络电视wifi密码怎么改
- erp系统开发用前端吗,erp系统用什么软件开发
- 手机虎牙怎么开电影直播,手机虎牙怎么开电影直播教程
- scp命令linux安装的简单介绍
- mysqlshell连接数据库操作,mysql连接sqlserver数据库
- 手机子账号怎么注销,子账号手机号留谁的?
- js局部放大镜的实现原理,js图片局部放大
- vb.net文字特效 vbs特效代码