java关闭系统程序

import java.io.IOException;  
import java.awt.event.*; 
import javax.swing.*; 
public class CtrWDS extends JFrame implements ActionListener{   
    JButton restart,shutdown,logout; 
    Box boxh; 
    private CtrWDS(String s) { 
        super(s); 
        restart=new JButton("重起"); 
        shutdown=new JButton("关机"); 
        logout=new JButton("注销"); 
        boxh=Box.createHorizontalBox(); 
        restart.addActionListener(this); 
        shutdown.addActionListener(this); 
        logout.addActionListener(this); 
        boxh.add(restart);  
        boxh.add(shutdown); 
        boxh.add(logout); 
        add(boxh); 
        addWindowListener(new WindowAdapter(){ 
            public void windowClosing(WindowEvent e) 
                    {                         
                      System.exit(0); 
                     } 
        }); 
    }      
    public void exec(String kind) { 
        try { 
            Runtime.getRuntime().exec("cmd /c start call shutdown -"+kind+" -f -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) { 
       CtrWDS ctr=new CtrWDS("关机控制"); 
       ctr.setBounds(300,0,200,65); 
       ctr.show(); 
    } 

} 

你可能感兴趣的:(java,C++,c,swing,C#)