java实现倒计时器-图形界面

本文介绍如何使用Java编程实现一个带有图形界面的倒计时器应用,涵盖了Swing库的使用,事件监听以及时间管理等核心概念。
摘要由CSDN通过智能技术生成

 

 

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 倒计时
 */
public class Counter {

    private JFrame frame;
    private JLabel jl0;

    private ScheduledThreadPoolExecutor scheduled;

    public static void main(String[] args) {
        new Counter().timer("2017-09-20 21:06:00");

    }

    /* String -> Date */
    private Date String2Date(String dateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(dateStr);
            if (date.getTime() <= System.currentTimeMillis()) {
                jl0.setText("时间不能早于现在" + dateStr);
    
  • 20
    点赞
  • 113
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是使用Java图形化界面实现简易计时器的示例代码: ``` import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TimerApp implements ActionListener { private JFrame frame; private JLabel label; private JButton startButton, stopButton, resetButton; private Timer timer; private int seconds = 0, minutes = 0, hours = 0; public TimerApp() { frame = new JFrame("Timer App"); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); label = new JLabel("00:00:00"); label.setFont(new Font("Verdana", Font.BOLD, 50)); frame.add(label); startButton = new JButton("Start"); startButton.addActionListener(this); frame.add(startButton); stopButton = new JButton("Stop"); stopButton.addActionListener(this); frame.add(stopButton); resetButton = new JButton("Reset"); resetButton.addActionListener(this); frame.add(resetButton); timer = new Timer(1000, this); frame.setSize(300, 200); frame.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == startButton) { timer.start(); } else if (e.getSource() == stopButton) { timer.stop(); } else if (e.getSource() == resetButton) { timer.stop(); seconds = 0; minutes = 0; hours = 0; updateLabel(); } else if (e.getSource() == timer) { seconds++; if (seconds == 60) { minutes++; seconds = 0; } if (minutes == 60) { hours++; minutes = 0; } updateLabel(); } } private void updateLabel() { String secs = String.format("%02d", seconds); String mins = String.format("%02d", minutes); String hrs = String.format("%02d", hours); label.setText(hrs + ":" + mins + ":" + secs); } public static void main(String[] args) { new TimerApp(); } } ``` 运行该程序,即可看到一个简易的计时器界面,包含了开始、停止、重置功能。用户点击开始按钮后,计时器开始计时,每秒钟更新一次显示,直到用户点击停止按钮。用户点击重置按钮后,计时器重置为零。 该程序使用了Java的Swing库来实现图形化界面,使用了计时器Timer类来实现计时功能。在每秒钟的更新中,程序会检查是否需要更新分钟和小时,然后调用updateLabel()方法来更新显示。该方法使用了String.format()方法来格式化时间字符串,确保时间显示为两位数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值