java桌面小闹钟

需求

为防止整天久坐,编写一个桌面闹钟.该闹钟功能很简单,一个小时的倒计时,倒计时结束弹框提示起身运动下.点击确认后重新计时.且有一个复位按钮,可以重新计时.

代码

  1. 定义一个1小时的时间变量
  2. 创建一个GUI界面,有一个Label标签显示时间,一个Button进行复位操作
  3. 计时器每秒判断时间变量是否大于0.大于0减1,否则弹框提示并对时间变量复原
  4. 弹框点确认可以实现复位键的同样效果

代码如下:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;

public class AlarmClock {

private static int countdownTime = 60*60 ; //一小时倒计时
private static int countTime = 0;
private static JFrame frame;
private static JLabel timeLabel;
private static JButton resetButton;
private static JLabel uptimeLabel;

public static void main(String[] args) {
createdUI();
}

private static void createdUI(){
frame = new JFrame("电脑桌面闹钟");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
frame.setLayout(new FlowLayout());

timeLabel = new JLabel("剩余时间:"+countdownTime/60 +"分钟"+countdownTime%60+"秒;计时:");
frame.add(timeLabel);

uptimeLabel = new JLabel("00:00:00",SwingConstants.CENTER);
frame.add(uptimeLabel);

resetButton = new JButton("复位");
resetButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
countdownTime = 60*60 ; // 重新开始一小时计时
countTime = 0;
updateTimeLabel();
}
});
frame.add(resetButton);

Timer timer = new Timer(1000,new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (countdownTime > 0) {
countdownTime --;
countTime++;
updateTimeLabel();
}else{
showDialog(frame);
}
}
});
timer.start();

frame.setVisible(true);
}

private static void updateTimeLabel(){
timeLabel.setText("剩余时间:"+countdownTime/60+"分钟"+countdownTime%60+"秒");
int hours = countTime /3600;
int minutes = (countTime % 3600)/60;
int remainingSeconds = countTime % 60;
uptimeLabel.setText(String.format("%02d:%2d:%2d", hours,minutes,remainingSeconds));
}

private static void showDialog(JFrame parent){
JDialog jDialog = new JDialog(parent,"确认对话框",true);
jDialog.setSize(200,100);

JButton okButton = new JButton("确认");
okButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == okButton) {
countdownTime = 60*60 ;
countTime = 0;
timeLabel.setText("剩余时间:"+countdownTime/60+"分钟"+countdownTime%60+"秒");
uptimeLabel.setText("00:00:00");
}
jDialog.dispose();
}
});

JPanel panel = new JPanel();
panel.add(okButton);
jDialog.getContentPane().add(panel,BorderLayout.CENTER);

//获取屏幕尺寸和对话框尺寸,计算居中位置
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screenSize.width - jDialog.getWidth()) /2 ;
int y = (screenSize.height - jDialog.getHeight()) / 2;
//设置对话框位置
jDialog.setLocation(x,y);

//设置对话框始终位于顶部
jDialog.setAlwaysOnTop(true);
//设置对话框为模态对话框
jDialog.setModal(true);

jDialog.setVisible(true);
}
}

编译运行

打开dos命令窗口进入当前文件夹,执行以下命令(用开发工具导出jar包更便捷,下述命令方式只为记录.)

1. 编译

javac -encoding UTF-8 AlarmClock.java

2. 生成jar文件

jar cvfe MyClock.jar AlarmClock *.class

3. 运行

java -jar MyClock.jar
为便于使用,可直接写bat处理文本.
新建一个MyClock.bat的文件,文件内容为
START java.exe -jar MyClock.jar
将该bat文件发送到桌面快捷方式,每次双击该bat程序即可.

遇到问题记录

1. 编译报错

错误:找不到或无法加载主类
解决:编写代码在开发软件中,没有定义包,但有默认src路径,在类文件当前路径编译运行报错.将文件包引入删除即可.

2. jar文件执行错误

错误:直接在eclipse中导出jar包,运行java -jar命令报错"jar中没有主清单属性"
解决:导出jar包选项错误.eclipse-export-java有三个选项.jar file,javadoc,runnable jar file.需要选择runnable jar file进行导出.

posted @ 2024-01-05 14:26  文依悠然  Views(238)  Comments(0)    收藏  举报