import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.io.*;public class AutoShutdown extends JFrame implements ActionListener,Runnable{
private JLabel label1,label2,leave;
private JButton start,stop;
private JTextField jhour,jminute,jsecond;

public AutoShutdown(){
super("AutoShutdown v1.0");
this.setSize(300,200);
this.setLocation(500,300);
this.setResizable(false);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setLayout(null);

jhour=new JTextField("00");
jhour.setBounds(70, 20, 30, 20);
jminute=new JTextField("00");
jminute.setBounds(110, 20, 30, 20);
jsecond=new JTextField("00");
jsecond.setBounds(150, 20,30,20);

jhour.selectAll();
jminute.selectAll();
jsecond.selectAll();


label1=new JLabel("离关机还有:");
label1.setBounds(10,50,100,30);
label2=new JLabel("关机时间:");
label2.setBounds(10,20, 100, 20);

start=new JButton("开始运行");
start.setBounds(180,50,100,30);
stop=new JButton("停止运行");
stop.setBounds(180,100,100,30);

this.add(jhour);
this.add(jminute);
this.add(jsecond);
this.add(label1);
this.add(label2);
this.add(start);
this.add(stop);

start.addActionListener(this);
stop.addActionListener(this);
this.setVisible(true);
}

public void getDateMonth(){
int hour,minute,second,close_h,close_m,close_s;
boolean key=true;
try{

close_h=Integer.parseInt(jhour.getText());
close_m=Integer.parseInt(jminute.getText());
close_s=Integer.parseInt(jsecond.getText());

if(close_h>23||close_m>59||close_s>59){
key=false;
//leave.setText("时间设置错误!");
}else{
key=true;
}
jhour.setEditable(false);
jminute.setEditable(false);
jsecond.setEditable(false);

while(true&&key){
hour=Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
minute=Calendar.getInstance().get(Calendar.MINUTE);
second=Calendar.getInstance().get(Calendar.SECOND);
System.out.println(hour+":"+minute+":"+second);
//leave.setText(String.valueOf(hour)+":"+String.valueOf(minute)+":"+String.valueOf(second));
if(close_h==hour&&close_m==minute&&close_s==second){
run();
break;
}
Thread.sleep(1000);
}


}catch (NumberFormatException e) {
leave.setText("时间格式不正确");
} catch (HeadlessException e1) {
e1.printStackTrace();
}catch (InterruptedException e2) {
e2.printStackTrace();
}
}

public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(start)) {
getDateMonth();
} else{
jhour.setEditable(true);
jminute.setEditable(true);
jsecond.setEditable(true);
exec("shutdown -a");
}
}
public void run(){
exec("shutdown -s");
}
private void exec(String cmd) {
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
}

    public static void main(String args[]){
     AutoShutdown auto=new AutoShutdown();
    }
}//点击开始运行后再点关闭窗口,停止运行就没反应了,
//NumberFormatException好像也不会抛出异常,
//leave那里也显示不了文本。
望高手解答,谢了。