- 论坛徽章:
- 0
|
作者:rqfreedom
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.IOException;
public class chongqi implements ActionListener{
static JFrame frm=new JFrame("关机程序");
static JButton shutdown=new JButton("关机");
static JButton rest=new JButton("重启");
static JButton logout=new JButton("注销");
public chongqi(){
frm.getContentPane().setLayout(new GridLayout(1,3));
frm.getContentPane().add(shutdown);
frm.getContentPane().add(rest);
frm.getContentPane().add(logout);
shutdown.addActionListener(this);
rest.addActionListener(this);
logout.addActionListener(this);
frm.pack();
frm.show();
}
public static void exec(String cmd) {
try {
Runtime.getRuntime().exec(cmd);
}
catch (IOException e) {
System.out.println("执行失败");
}
}
public static void shutdown() {
exec("shutdown -S ");
}
public static void restart() {
exec("shutdown -R ");
}
public static void logout() {
exec("shutdown -L ");
}
public static void main(String args[]){
new chongqi();
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("关机"))
shutdown();
else if(e.getActionCommand().equals("重启"))
restart();
else if(e.getActionCommand().equals("注销"))
logout();
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/11570/showart_82316.html |
|