import java.io.IOException;
import java.awt.event.*;
import javax.swing.*;
public class ShutDown extends JFrame implements ActionListener{ 
    JButton restart,shutdown,logout;
    Box box;
    public ShutDown(String s) {
        super(s);
        restart=new JButton("重起");
        shutdown=new JButton("关机");
        logout=new JButton("注销");
        box=Box.createHorizontalBox();
        restart.addActionListener(this);
        shutdown.addActionListener(this);
        logout.addActionListener(this);
        box.add(restart);
        box.add(shutdown);
        box.add(logout);
        add(box);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e)
                    {                       
                      System.exit(0);
                     }
        });
    }   
    public void exec(String choice) {
        try {
            Runtime.getRuntime().exec("cmd /c start call shutdown -"+choice+" -t 0");
        }
        catch (IOException e) {
            System.out.println("执行失败");       
        }
    }
    public void shutdown() {
        exec("S");
    }
    public void restart() {
        exec("R");
    }
    public void logout() {
        exec("L");
    }
  public void actionPerformed(ActionEvent e)
    {
      if(e.getSource()==shutdown)
        { 
           shutdown(); 
        }
        else if(e.getSource()==restart)
        {
           restart();
        }
        else if(e.getSource()==logout)
        {
           logout();
        }
    }
    public static void main(String[] str) {
       ShutDown ctr=new ShutDown("关机控制");
       ctr.setBounds(300,0,200,65);
       ctr.setVisible(true);
    }
}
 
	posted on 2008-06-27 08:52 
greedy 阅读(213) 
评论(0)  编辑  收藏  所属分类: 
Java技术