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