一個java關機程式

來源:互聯網
上載者:User
  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.   
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.