展开全部
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();
}
}
}
}
}
展开全部
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() {
//你要进行的操作
}
}
}
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() {
//你要进行的操作
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询