JAVA编写的一个自动关机软件

/*定时关机程序*/
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{
  JLabel copyright,jlclew1,jlhour,jlminute,jlclew2,leave;
  JTextField jthour,jtminute;
  JButton run,exit;
  boolean falg=false;
  public AutoShutdown(){
  super("自动关机");
  }
  //窗体布局
  public void setPane(){
  copyright=new JLabel("Star",JLabel.CENTER);
  copyright.setBounds(50,5,200,50);

  jlclew1=new JLabel("系统将于:");
  jlclew1.setBounds(10,65,80,30);  
  jthour=new JTextField();
  jthour.setBounds(90,65,60,30);
   
  jlhour=new JLabel(" 时 ");
  jlhour.setBounds(150,65,30,30);  
  jtminute=new JTextField();
  jtminute.setBounds(180,65,60,30);
  jlminute=new JLabel(" 分 ");
  jlminute.setBounds(240,65,30,30);
  jlclew2=new JLabel("后关机");
  jlclew2.setBounds(270,65,80,30);  

  leave=new JLabel("请输入时间",JLabel.CENTER);
  leave.setBounds(50,100,200,50);
   
  run=new JButton("开始运行");
  run.setBounds(60,160,100,30);
  run.addActionListener(this);
  exit=new JButton("结束");
  exit.setBounds(180,160,100,30);
  exit.addActionListener(this);
   
  Container ct=this.getContentPane();
  ct.setLayout(null);
  ct.add(copyright);
  ct.add(jlclew1);
  ct.add(jthour);
  ct.add(jlhour);
  ct.add(jtminute);
  ct.add(jlminute);
  ct.add(jlclew2);
  ct.add(leave);
  ct.add(run);
  ct.add(exit);
  this.setSize(350,230);
  this.setVisible(true);
  }
   
  /*public void shutdown(){
  Runtime rt=Runtime.getRuntime();
  String str="shutdown -l";
  try{
  rt.exec("shutdown -a");  
  }catch(IOException e){System.out.println("调用关机命令时系统出错");}
  System.exit(0);
  }  
   
  */
  public void getDateMonth(){
  int hour,minute,dthour,dtminute,dt1,dt2;
  //dthour 表示当前小时 
  //dtminute 表示当前分钟
  //dt1 表示程序将在某小时关机
  //dt2 表示程序将在某分钟关机
  try{
  if(jthour.getText().equals("")){
  minute=Integer.parseInt(jtminute.getText());
  hour=0;
  }
  else if(jthour.getText().equals("")){
  hour=Integer.parseInt(jthour.getText());
  minute=0;
  }
  else{
  minute=Integer.parseInt(jtminute.getText());
  hour=Integer.parseInt(jthour.getText());
  }
  //获取要关机的时间
  dt1=new Date().getHours()+hour;
  dt2=new Date().getMinutes()+minute;
  // this.setVisible(false);  
   
  while(true){ 不用while行吗?太费资源。
  //获取当前时间
  dthour=new Date().getHours();  
  dtminute=new Date().getMinutes();  
   
  System.out.println(dthour+" "+dtminute+" "+(new Date().getSeconds()));
  //为什么leave.setText()显示不出来倒计时呀。
  leave.setText(((dt1-dthour)*3600+(dt2-dtminute)*60)+"后关机");
  //系统关机提示
  if((dt2-dtminute)<=2 && (dt1-dthour)==0 && !falg){  
  int a=JOptionPane.showConfirmDialog(null,"程序将在2分钟以内关机您确定吗?","关机提示",0,0 );
  if(a==1){
  AutoShutdown as=new AutoShutdown();
  as.setPane();
  break;
  }
  falg=true;
  }
  if(dt1==dthour && dt2==dtminute){
  run();
  break;
  }  
   
  Thread.sleep(60000);
  }
  }catch(NumberFormatException e){leave.setText("时间格式不正确");}
  catch(HeadlessException e2){e2.printStackTrace(); }
  catch(InterruptedException e1){e1.printStackTrace();}  
  return;
  }
  //设置按钮监听
  public void actionPerformed(ActionEvent e) {
  if(e.getSource().equals(run)){
  getDateMonth();
  }
  else
  System.exit(0);  
  }
  public static void main(String args[]){
  AutoShutdown as=new AutoShutdown();
  as.setPane();
  }
  @Override
  //调用关机命令
  public void run() {  
  exec("shutdown -r -f");
  }
  private void exec(String cmd) {  
  try {
  Runtime.getRuntime().exec(cmd);
  } catch (IOException e) {
  e.printStackTrace();
  }
  }

}

你可能感兴趣的:(java,thread,swing,F#)