java 重启系统_java实现系统的关闭,重启

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值