Java小闹钟,运行于网页上的小闹钟程序

import java.util.Calendar;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import com.junlts.util.SwingUtil;

public class Clock extends javax.swing.JFrame {

    private int hour;
    private boolean isAlarmOn;

    private ImageIcon clockImageOn, clockImageOff;

    private JLabel clockLabel;

    private boolean isAlarm;

    public Clock() {
        hour = 0;
        isAlarmOn = false;
        isAlarm = false;

        clockImageOn = new ImageIcon("images/clock_on.png");
        clockImageOff = new ImageIcon("images/clock_off.png");

       	clockLabel = new JLabel("00:00");

        setupUI();
    }

    private void setupUI() {
        this.setIconImage(ClassLoader.getSystemResourceAsStream("images/icon.png"));
        this.setTitle("Java Clock");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);

        clockLabel.setFont(new java.awt.Font("Segoe UI", 1, 60));
        clockLabel.setBounds(50, 50, 350, 150);
        this.add(clockLabel);

        JButton clockButton = new JButton("Start");
        clockButton.addActionListener(e -> {
            if (clockButton.getText().equals("Start")) {
                clockButton.setText("Stop");
                clockButton.repaint();
                new Timer(1000, (EventObject e1) -> {
                    updateClock();
                }).start();
            } else {
                clockButton.setText("Start");
                clockButton.repaint();
                Timer timer = (Timer) e1;
                timer.stop();
            }
        });
        clockButton.setBounds(75, 250, 150, 50);
        this.add(clockButton);

        JButton alarmButton = new JButton("Set Alarm");
        alarmButton.addActionListener(e -> {
            int hour = JOptionPane.showConfirmDialog(this, "Enter hour (0-23):", "Set Alarm", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (hour == JOptionPane.OK_OPTION) {
                this(hour);
                isAlarmOn = true;
                alarmButton.setText("Alarm is set");
                alarmButton.repaint();
            } else {
                alarmButton.setText("Set Alarm");
                alarmButton.repaint();
            }
        });
        alarmButton.setBounds(250, 250, 150, 50);
        this.add(alarmButton);

        JButton playButton = new JButton("Play Sound");
        playButton.addActionListener(e -> {
            if (isAlarm) {
                javax.sound.sampled.Clip clip = AudioPlayer.playSound("sounds/alarm.wav");
                clip.start();
            }
        });
        playButton.setBounds(75, 320, 150, 50);
        this.add(playButton);

        SwingUtil.centerWindowOnScreen(this);
        this.setVisible(true);
    }

    private void updateClock() {
        Calendar calendar = Calendar.getInstance();
        hour = calendar.get(Calendar.HOUR);
        if (isAlarm && hour == 6) {
            isAlarm = false;
        }
        clockLabel.setText(String.format("%02d:%02d", hour % 12, hour / 12));
        clockLabel.repaint();
    }

    private void setHour(int hour) {
        this.hour = hour;
        repaint();
    }

    private boolean isAlarmOn() {
        return isAlarmOn;
    }

    private boolean isAlarm() {
        return isAlarm;
    }

    public static void main(String[] args) {
        SwingUtil.invokeLater(Clock::new);
    }
}

上述代码是一个Java小闹钟程序。程序的主要逻辑如下:

  1. 创建了一个名为Clock的类,该类继承自javax.swing.JFrame,表示一个窗口。
  2. Clock类中定义了以下几个成员变量:
    • hour:表示当前时间的小时数。
    • isAlarmOn:表示是否打开了闹钟。
    • clockImageOnclockImageOff:表示开启和关闭状态的时钟图片。
    • clockLabel:表示显示时间的标签。
    • isAlarm:表示是否到了设定的闹钟时间。
  3. Clock类的构造方法中,初始化了上述成员变量,并调用setupUI()方法来设置界面。
  4. setupUI()方法主要做了以下事情:
    • 设置窗口的图标,并设置标题为"Java Clock"。
    • 设置窗口关闭的模式为EXIT_ON_CLOSE
    • 将界面布局设置为null布局。
    • 创建一个JLabel实例,并设置显示文本为"00:00",并将其添加到窗口中。
    • 创建一个JButton实例clockButton,用于启动或停止定时器,设置点击事件的处理方法为clockButtonActionListener,并将其添加到窗口中。
    • 创建一个JButton实例alarmButton,用于设置闹钟,设置点击事件的处理方法为alarmButtonActionListener,并将其添加到窗口中。
    • 创建一个JButton实例playButton,用于播放闹钟声音,设置点击事件的处理方法为playButtonActionListener,并将其添加到窗口中。
    • 调用SwingUtil.invokeLater()方法来确保界面的显示是线程安全的,并将Clock类的构造方法作为参数传入,以便在事件线程中创建Clock对象。
  5. updateClock()方法用于更新时间显示,方法内部逻辑如下:
    • 获取当前时间的小时数。
    • 如果闹钟打开且当前时间是早上6点,则将isAlarm设为false,表示闹钟已响。
    • 根据更新后的小时数,格式化为"hh:mm"的形式,然后设置clockLabel的显示文本。
    • 刷新clockLabel组件,使其显示更新后的内容。
  6. setHour(int hour)方法用于设置时间,方法接受一个表示小时数的参数,并将其赋值给成员变量hour。然后调用repaint()方法刷新组件。
  7. isAlarmOn()方法用于判断闹钟是否打开,方法返回成员变量isAlarmOn的值。
  8. isAlarm()方法用于判断是否到了闹钟时间,方法返回成员变量isAlarm的值。
  9. main()方法中,通过调用SwingUtil.invokeLater()方法来创建Clock对象,实现线程安全的界面显示。
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JOZECN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值