发新话题
移动帖子 加入精华 加入置顶 加入收藏 关注此帖

一个java关机程序



一个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();
    }

}
快乐渡过每一天,减肥坚持每一天
编辑 回复 快速回复 TOP

Re:一个java关机程序

非常不错,好东东!
不过,这个程序好像在Windws下运行才有效吧。
Never surrender to complexity ——永远别向复杂低头
编辑 回复 快速回复 TOP
发新话题