日期:2014-05-19 浏览次数:20740 次
package huatu; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import javax.swing.*; public class ClockFrame extends JFrame { 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; public ClockFrame() { setTitle("关机定时"); setSize(200, 150); Container contentPanel = getContentPane(); JPanel timePanel = new JPanel(); timePanel.setLayout(new GridLayout(4, 2)); JLabel minuteLable = new JLabel("设置分钟"); timePanel.add(minuteLable); minuteBox = new JComboBox(); timePanel.add(minuteBox); for (int i = 0; i < 181; 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 < 80; 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) { JButton check=(JButton) evt.getSource(); if (check.getText().equals("确定")) { 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 = (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 \"我要关机了噢!不好意思!\" -t " + totalSeconds); check.setText("取消"); } catch (Exception e) { e.printStackTrace(); } }else{ try { Runtime.getRuntime().exec("shutdown.exe -a"); check.setText("确定"); } catch (Exception e) { e.printStackTrace(); } } } }); } private void hideFrame() { this.setVisible(false); } public static void main(String[] args) {