java倒计时 暂停 重置_Java,如何给这个倒计时器加个暂停按钮

packagechess;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;/***倒计时**/publicclassCounter{privateJFrameframe;privateJLabelt1;privateJLabelt2...

package chess;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

/**

* 倒计时

*

*/

public class Counter {

private JFrame frame;

private JLabel t1;

private JLabel t2;

private JLabel t3;

public static void main(String[] args) {

new Counter().getTime();

}

/* 倒计时的主要代码块 */

private void getTime() {

long time = 1 * 1800; // 自定义倒计时时间

long hour = 0;

long minute = 0;

long seconds = 0;

while (time >= 0) {

hour = time / 3600;

minute = (time - hour * 3600) / 60;

seconds = time - hour * 3600 - minute * 60;

t1.setText(hour + ":");

t2.setText(minute + ":");

t3.setText(seconds + ":");

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

time--;

}

}

/* 构造 实现界面的开发 GUI */

public Counter() {

frame = new JFrame("倒计时");

t1 = new JLabel();

t2 = new JLabel();

t3 = new JLabel();

init();

}

/* 组件的装配 */

private void init() {

JPanel jp = new JPanel();

jp.add(t1);

jp.add(t2);

jp.add(t3);

frame.add(jp);

frame.setVisible(true);

frame.setLocation(300, 400);

frame.setSize(300, 200);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

展开

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Java计时器窗口的示例代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TimerWindow extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JLabel timeLabel; private Timer timer; private int timeCount = 0; private JButton startButton, stopButton, resetButton; public TimerWindow() { setTitle("计时器"); setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); timeLabel = new JLabel("00:00:00"); timeLabel.setFont(new Font("宋体", Font.PLAIN, 50)); timeLabel.setHorizontalAlignment(SwingConstants.CENTER); add(timeLabel, BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); startButton = new JButton("开始"); startButton.addActionListener(this); buttonPanel.add(startButton); stopButton = new JButton("暂停"); stopButton.addActionListener(this); buttonPanel.add(stopButton); resetButton = new JButton("重置"); resetButton.addActionListener(this); buttonPanel.add(resetButton); add(buttonPanel, BorderLayout.SOUTH); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == startButton) { if (timer == null) { timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { timeCount++; int hour = timeCount / 3600; int minute = (timeCount - hour * 3600) / 60; int second = timeCount - hour * 3600 - minute * 60; timeLabel.setText(String.format("%02d:%02d:%02d", hour, minute, second)); } }); timer.start(); } else { timer.start(); } } else if (e.getSource() == stopButton) { if (timer != null) { timer.stop(); } } else if (e.getSource() == resetButton) { timeCount = 0; timeLabel.setText("00:00:00"); if (timer != null) { timer.stop(); } } } public static void main(String[] args) { TimerWindow frame = new TimerWindow(); frame.setVisible(true); } } ``` 这个计时器窗口包含一个用于显示计时时间的标签,三个按钮用于开始、暂停重置计时器计时器使用了Javajavax.swing.Timer类,每秒钟更新一次计时时间。当用户单击开始按钮时,计时器启动并开始计时;当用户单击暂停按钮时,计时器暂停;当用户单击重置按钮时,计时器归零。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值