java秒表计时器_Java-计时器/秒表GUI

问题是您的应用程序中只有一个线程.您应该至少有两个:一个用于更新文本的UI,另一个用于计算时间.

如果只有一个线程,它将挂在while(true)循环中,而Swing永远无法更新视图.

我使用两个线程重构了您的代码:

>一次计数直到时间结束,并更新字段以将时间保留在内存中

>每隔100毫秒调用一个使用java.util.Timer#scheduleAtFixedRate()方法的方法来更新视图.

Timer.java(避免像Java API中那样命名类)

public class Timer {

boolean shouldCount=false;

int int_sec=0;

int int_min=0;

int int_mil=0;

public Timer() {

}

public static void main(String[] args) {

TimeFrame t = new TimeFrame();

JFrame f = new JFrame("Timer");

f.setSize(300,200);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setLocationRelativeTo(null);

f.getContentPane().add(new TimeFrame());

f.setVisible(true);

}

public void count(){

Thread thread = new Thread(new Runnable() {

@Override

public void run() {

long now = System.currentTimeMillis();

while(true){

if(shouldCount){

if(System.currentTimeMillis()-now>=100){

now=System.currentTimeMillis();

int_mil++;

if(int_mil>9){

int_mil=0;

int_sec++;

if(int_sec>=60){

int_sec=1;

int_min++;

}

}

}

}

}

}

});

thread.start();

}

}

}

TimeFrame(我更喜欢将其称为TimePanel,因为它扩展了JPanel)

public class TimeFrame extends JPanel{

JLabel time;

Timer t ;

JButton pause ;

JButton start ;

public TimeFrame(){

t= new Timer(this);

time = new JLabel("Time goes here", JLabel.CENTER);

pause = new JButton ("Pause");

start = new JButton ("Start");

start.addActionListener(new starts());

pause.addActionListener(new starts());

add(time);

add(start);

add(pause);

java.util.Timer updateTimer= new java.util.Timer();

updateTimer.scheduleAtFixedRate(new TimerTask() {

@Override

public void run() {

t.update(int_sec,int_min,int_mil);

}

}, 0, 100);

}

public void update(int s, int minute,int m){

String sec = Integer.toString(s);

String min = Integer.toString(minute);

String mil = Integer.toString(m);

if (s<=10){

sec="0"+sec;

}

System.out.println(min+":"+sec+","+mil);

time.setText(min+":"+sec+","+mil);

}

public class starts implements ActionListener{

boolean firstTime=true;

public void actionPerformed(ActionEvent event){

if (firstTime){

t.count();

firstTime = false;

}

if(event.getSource() == start){

t.shouldCount=true;

}else{

t.shouldCount=false;

}

}

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现秒表计时器的示例代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Stopwatch implements ActionListener { private JFrame frame; private JLabel timeLabel; private JButton startButton, stopButton, resetButton; private Timer timer; private int elapsedTime; private int seconds; private int minutes; private int hours; private boolean started; public Stopwatch() { frame = new JFrame("Stopwatch"); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); timeLabel = new JLabel("00:00:00", JLabel.CENTER); startButton = new JButton("Start"); stopButton = new JButton("Stop"); resetButton = new JButton("Reset"); startButton.addActionListener(this); stopButton.addActionListener(this); resetButton.addActionListener(this); timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { elapsedTime += 1000; updateTimer(); } }); JPanel panel = new JPanel(new GridLayout(2, 1)); JPanel topPanel = new JPanel(new GridLayout(1, 1)); JPanel bottomPanel = new JPanel(new GridLayout(1, 3)); topPanel.add(timeLabel); bottomPanel.add(startButton); bottomPanel.add(stopButton); bottomPanel.add(resetButton); panel.add(topPanel); panel.add(bottomPanel); frame.add(panel); frame.setVisible(true); } public static void main(String[] args) { new Stopwatch(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == startButton) { start(); } else if (e.getSource() == stopButton) { stop(); } else if (e.getSource() == resetButton) { reset(); } } private void start() { if (!started) { started = true; timer.start(); } } private void stop() { if (started) { started = false; timer.stop(); } } private void reset() { started = false; timer.stop(); elapsedTime = 0; seconds = 0; minutes = 0; hours = 0; updateTimer(); } private void updateTimer() { seconds = (elapsedTime / 1000) % 60; minutes = (elapsedTime / 60000) % 60; hours = (elapsedTime / 3600000) % 24; String secondsStr = String.format("%02d", seconds); String minutesStr = String.format("%02d", minutes); String hoursStr = String.format("%02d", hours); timeLabel.setText(hoursStr + ":" + minutesStr + ":" + secondsStr); } } ``` 这个示例代码实现了一个基本的秒表计时器功能,包括开始、停止和重置功能。它使用了Java Swing中的JFrame、JLabel、JButton和JPanel组件,以及Timer类来实现计时器的功能。当用户点击开始按钮时,计时器会开始计时;当用户点击停止按钮时,计时器会停止计时;当用户点击重置按钮时,计时器会重置为0。计时器的显示格式为:小时:分钟:秒钟。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值