一个java关机程序

  1. import java.io.IOException; 
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class CtrWDS extends JFrame implements ActionListener{  
  5.     JButton restart,shutdown,logout;
  6.     Box boxh;
  7.     private CtrWDS(String s) {
  8.         super(s);
  9.         restart=new JButton("重起");
  10.         shutdown=new JButton("关机");
  11.         logout=new JButton("注销");
  12.         boxh=Box.createHorizontalBox();
  13.         restart.addActionListener(this);
  14.         shutdown.addActionListener(this);
  15.         logout.addActionListener(this);
  16.         boxh.add(restart); 
  17.         boxh.add(shutdown);
  18.         boxh.add(logout);
  19.         add(boxh);
  20.         addWindowListener(new WindowAdapter(){
  21.             public void windowClosing(WindowEvent e)
  22.                     {                        
  23.                       System.exit(0);
  24.                      }
  25.         });
  26.     }     
  27.     public void exec(String kind) {
  28.         try {
  29.             Runtime.getRuntime().exec("cmd /c start call shutdown -"+kind+" -f -t 0");
  30.         } 
  31.         catch (IOException e) {
  32.             System.out.println("执行失败");        
  33.         }
  34.     }
  35.     public void shutdown() {
  36.         exec("S");
  37.     }
  38.     public void restart() {
  39.         exec("R");
  40.     }
  41.     public void logout() {
  42.         exec("L");
  43.     }
  44.   public void actionPerformed(ActionEvent e)
  45.     {
  46.       if(e.getSource()==shutdown)
  47.         {  
  48.            shutdown();  
  49.         }
  50.         else if(e.getSource()==restart)
  51.         {
  52.            restart();
  53.         }
  54.         else if(e.getSource()==logout)
  55.         {
  56.            logout();
  57.         }
  58.     }
  59.     public static void main(String[] str) {
  60.        CtrWDS ctr=new CtrWDS("关机控制");
  61.        ctr.setBounds(300,0,200,65);
  62.        ctr.show();
  63.     }
  64. }
  65.   
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值