包含java计时10秒代码的词条

java等待10秒在运行怎么写代码Thread.sleep(10000).
交出线程占用CPU时间十秒钟.
单位是毫秒.
怎样用JAVA写一个10钟倒计时程序?import java.awt.Color;
import java.util.*;
import java.awt.*;
import java.applet.*;
public class Clock extends Applet implements Runnable
{
Thread timer=null;
Label label;
int lastxs=50,lastys=30,lastxm=50,lastym=30,lastxh=50,lastyh=30;
public void init()
{
label=new Label(" ");
setBackground(Color.white);
add(label);
}
public void paint(Graphics g)
{
int xh,yh,xm,ym,xs,ys,s,m,h,xcenter,ycenter;
Date rightnow=new Date();
String today=rightnow.toLocaleString();
label.setText(today);
s=rightnow.getSeconds();
m=rightnow.getMinutes();
h=rightnow.getHours();
xcenter=100;
ycenter=80;
xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*45 xcenter);
ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*45 ycenter);
xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*45 xcenter);
ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*45 ycenter);
xh=(int)(Math.cos((h*30 m*2)*3.14f/180-3.14f/2)*30 xcenter);
yh=(int)(Math.sin((h*30 m*2)*3.14f/180-3.14f/2)*30 ycenter);
g.setFont(new Font("TimesToman",Font.PLAIN,14));
g.setColor(Color.orange);
g.fill3DRect(xcenter-50,ycenter-50,100,100,true);
g.setColor(Color.darkGray);
g.drawString("12",xcenter-5,ycenter-37);
g.drawString("3",xcenter 40,ycenter 3);
g.drawString("6",xcenter-3,ycenter 45);
g.drawString("9",xcenter-45,ycenter 3);
g.setColor(Color.orange);
if(xs!=lastxs||ys!=lastys)
{
g.drawLine(xcenter,ycenter,lastxs,lastys);
}
if(xm!=lastxm||ym!=lastym)
{
g.drawLine(xcenter,ycenter-1,lastxm,lastym);
g.drawLine(xcenter-1,ycenter,lastxm,lastym);
}
if(xh!=lastxh||yh!=lastyh)
{
g.drawLine(xcenter,ycenter-1,lastxh,lastyh);
g.drawLine(xcenter-1,ycenter,lastxh,lastyh);
}
g.setColor(Color.red);
g.drawLine(xcenter,ycenter,xs,ys);
g.drawLine(xcenter,ycenter-1,xm,ym);
g.drawLine(xcenter-1,ycenter,xm,ym);
g.drawLine(xcenter,ycenter-1,xh,yh);
g.drawLine(xcenter-1,ycenter,xh,yh);
lastxs=xs;
lastys=ys;
lastxm=xm;
lastym=ym;
lastxh=xh;
lastyh=yh;
}
public void start()
{
if(timer==null)
{
timer=new Thread(this);
timer.start();
}
}
public void stop()
{
timer=null;
}
public void run()
{
while(timer!=null)
{
try
{
Thread.sleep(1000);
}catch(InterruptedException ie){}
repaint();
}
timer=null;
}
public void update(Graphics g)
{
paint(g);
}
}
求人用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){
【包含java计时10秒代码的词条】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) {
// 窗口关闭前取出文本框的数字保存到外部文件,代码在此处写
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代码是什么?应该用线程里面的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计时10秒代码和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读