新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java计时代码函数 用java编写一个计时器程序

java有没有计时的函数?(类似于JavaScript的“setTimeout("……",1);”

刚回答过人

创新互联公司从2013年开始,先为汶上等服务建站,汶上等地企业,进行企业商务咨询服务。为汶上企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

public static void main(String[] args) {

Timer timer = new Timer();

timer.schedule(new TimerTask() {

@Override

public void run() {

// TODO Auto-generated method stub

System.out.println("执行");

}

}, 0, 1000);

// timer.cancel();

}

写一个计时器 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怎么给方法计时

你可以在开始和结束的时候,分别记录下当前的时间的这毫秒数。然后再减,以下是一段代码。

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计时代码函数 用java编写一个计时器程序
文章URL:http://www.wjwzjz.com/article/dosdhjj.html
在线咨询
服务热线
服务热线:028-86922220
TOP