呵呵,我下午刚写了一个小程序,仅供参考!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestAutoShutDown {
public static void main(String[] args) {
// TODO Auto-generated method stub
AutoShutDown frame = new AutoShutDown();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
class AutoShutDown extends JFrame {
private JLabel label;
private JTextField textField;
private JButton button;
private JPanel panel1;
public AutoShutDown(){
this.setTitle("自动关机");
label = new JLabel("分钟后关闭");
textField = new JTextField(8);
button = new JButton("确定");
button.addActionListener(new AutoCloseAction());
panel1 = new JPanel();
panel1.add(textField);
panel1.add(label);
panel1.add(button);
this.add(panel1);
}
class AutoCloseAction implements ActionListener{
private Calendar calendar;
@Override
public void actionPerformed(ActionEvent e) {
String time = textField.getText();
int timeInt = Integer.parseInt(time);
calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, timeInt);
String closeTime = "at "+calendar.get(Calendar.HOUR_OF_DAY) +":"+ calendar.get(Calendar.MINUTE);
System.out.println(closeTime);
String operation = closeTime + " shutdown -s -t 5";
System.out.println(operation);
Runtime rt = Runtime.getRuntime();
try {
rt.exec(operation);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
取消
评论