写了个桌面的小闹钟,在运行环境可以编译,但是打包成jar文件,想用批处理命令直接调用报错"找不到或无法加载主类".
为防止整天久坐,编写一个桌面闹钟.该闹钟功能很简单,一个小时的倒计时,倒计时结束弹框提示起身运动下.点击确认后重新计时.且有一个复位按钮,可以重新计时.
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命令窗口进入当前文件夹,执行以下命令
javac -encoding UTF-8 AlarmClock.java
jar cvfe MyClock.jar AlarmClock *.class
java -jar MyClock.jar
为便于使用,可直接写bat处理文本.
新建一个MyClock.bat的文件,文件内容为START java.exe -jar MyClock.jar
将该bat文件发送到桌面快捷方式,每次双击该bat程序即可.
错误:找不到或无法加载主类
解决:编写代码在开发软件中,没有定义包,但有默认src路径,在类文件当前路径编译运行报错.将文件包引入删除即可.