日期:2014-05-20  浏览次数:20797 次

谁会写让电脑关机的java程序啊
谁会写让电脑关机的java程序啊 ,会的高手们麻烦把代码贴出来吧,,我急用啊、、、

------解决方案--------------------
Java code

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;

class WindowOperations extends JFrame implements ActionListener{
  JRadioButton shut, restart, logoff;
  public WindowOperations(){
    super("Window");
    setSize(200,200);
    setResizable(false);
    setVisible(true);
    JPanel p1 = new JPanel();
    p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
    p1.setBorder(new TitledBorder(new EtchedBorder(), "Operations"));
    ButtonGroup group = new ButtonGroup();
    shut = new JRadioButton("Shutdown (F1)");
    restart = new JRadioButton("Restart (F2)");
    logoff = new JRadioButton("Logoff (F3)");
    group.add(shut);
    group.add(restart);
    group.add(logoff);
    shut.addActionListener(this);
    restart.addActionListener(this);
    logoff.addActionListener(this);
    p1.add(shut);
    p1.add(restart);
    p1.add(logoff);
    getContentPane().add(p1, BorderLayout.CENTER);
    JPanel p2 = new JPanel();
    JButton ok = new JButton("OK");
    ok.addActionListener(this);
    p2.add(ok);
    getContentPane().add(p2, BorderLayout.SOUTH);
    addKeyListener(new KeyAdapter(){
      public void keyPressed(KeyEvent ke){
        try{  
        switch(ke.getKeyCode()){
          case KeyEvent.VK_F1:
            Shutdown();
            break;
          case KeyEvent.VK_F2:
            Restart();
            break;  
          case KeyEvent.VK_F3:
            Logoff();
            break;
        }
        }catch(IOException e){}
      }
    });
    requestFocus();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((screenSize.width - getWidth())/2, (screenSize.height - getHeight())/2);
    pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
  public void Shutdown() throws IOException{
    Process p = Runtime.getRuntime().exec("shutdown -s -t 00");  
  }
  public void Restart() throws IOException{
    Process p = Runtime.getRuntime().exec("shutdown -r -t 00");  
  }
  public void Logoff() throws IOException{
    Process p = Runtime.getRuntime().exec("shutdown -l");  
  }
  public void actionPerformed(ActionEvent ae){
    String str = ae.getActionCommand();
    if(str.equals("OK")){
      try{
         if(shut.getText()=="Shutdown (F1)"){
           Shutdown();
         }
         if(restart.getText()=="Restart (F2)"){
           Restart();
         }
         if(logoff.getText()=="Logoff (F3)"){
           Logoff();
         }
      }catch(IOException e){}
    }
  }
  public static void main(String[] args) throws IOException{
    new WindowOperations();
  }
}

------解决方案--------------------
java 语言本身是无法做到关闭电脑的,想要实现关机的话需要调用 其他的语言 命令,像上面这个实例最关键的就是JVM开了一个新的进程 执行windows命令行的 shutdowm命令,所以上面只能适用于windows,如果linux需要调用linux的shell命令做

如果你windows的话直接用上面的就OK了,只是没有跨平台性
------解决方案--------------------
探讨
java 语言本身是无法做到关闭电脑的,想要实现关机的话需要调用 其他的语言 命令,像上面这个实例最关键的就是JVM开了一个新的进程 执行windows命令行的 shutdowm命令,所以上面只能适用于windows,如果linux需要调用linux的shell命令做

如果你windows的话直接用上面的就OK了,只是没有跨平台性

------解决方案--------------------
Runtime.getRuntime().exec("shutdown -s ")