📄 clock.java
字号:
/**
@version 1.00 2004-10
@author zzhuohuo
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
/**
Set the shutdowntime of windowns os
*/
public class Clock
{
public static void main(String[] args)
{
JFrame frame = new ClockFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.show();
}
}
/**
*/
class ClockFrame extends JFrame
{
public ClockFrame()
{
setTitle("关机定时");
setSize(WIDTH,HEIGHT);
Container contentPanel = getContentPane();
JPanel timePanel = new JPanel();
timePanel.setLayout(new GridLayout(4,2));
JLabel hourLable = new JLabel("设置时钟");
timePanel.add(hourLable);
hourBox = new JComboBox();
timePanel.add(hourBox);
for(int i = 0; i < 24; i++)
{
hourBox.addItem(i);
}
hourBox.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
hour = ((Integer)hourBox.getSelectedItem()).intValue();
}
});
JLabel minuteLable = new JLabel("设置分钟");
timePanel.add(minuteLable);
minuteBox = new JComboBox();
timePanel.add(minuteBox);
for(int i = 0; i < 60; i++)
{
minuteBox.addItem(i);
}
minuteBox.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
minute = ((Integer)minuteBox.getSelectedItem()).intValue();
}
});
JLabel secondLable = new JLabel("设置秒钟");
timePanel.add(secondLable);
secondBox = new JComboBox();
timePanel.add(secondBox);
for(int i = 0; i < 60; i++)
{
secondBox.addItem(i);
}
secondBox.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
second = ((Integer)secondBox.getSelectedItem()).intValue();
}
});
contentPanel.add(timePanel,BorderLayout.CENTER);
JButton check = new JButton("确定");
contentPanel.add(check,BorderLayout.SOUTH);
check.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
calendar = new GregorianCalendar();
int currentSeconds = calendar.get(Calendar.HOUR_OF_DAY) * 3600
+ calendar.get(Calendar.MINUTE) * 60
+ calendar.get(Calendar.SECOND);
totalSeconds = hour * 3600 + minute * 60 + second;
if (totalSeconds - currentSeconds < 0)
{
argue = (24 * 3600 + currentSeconds - totalSeconds)*1000;
JOptionPane.showMessageDialog(ClockFrame.this,"您设置的时间为 明天 " + hour + ":" + minute + ":" + second + "\n程序将在后台运行,并在第二日此时自动关闭计算机!","设置成功",JOptionPane.INFORMATION_MESSAGE);
hideFrame();
}
else
{
argue = (totalSeconds - currentSeconds) * 1000;
JOptionPane.showMessageDialog(ClockFrame.this,"您设置的时间为 " + hour + ":" + minute + ":" + second + "\n程序将在后台运行,并在此时自动关闭计算机!","设置成功",JOptionPane.INFORMATION_MESSAGE);
hideFrame();
}
try
{
Thread.sleep(argue);
Runtime.getRuntime().exec("shutdown.exe -s -c \"我要关机了噢!不要意思!\"");
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
private void hideFrame() {this.setVisible(false);}
private JComboBox hourBox,minuteBox,secondBox;
private int hour,minute,second,totalSeconds,currentSeconds;
private long argue;
private GregorianCalendar calendar;
private boolean change = true;
private static final int WIDTH=200;
private static final int HEIGHT=150;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -