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

未报告的异常错误IOException
import java.awt.*;
import java.awt.event.*;
import java.lang.Process;
import java.lang.Runtime;

class  ShutDown1
{
public static void main(String[] args) 
{
Frame frame = new Frame("定时关机");
frame.setBounds(400,300,200,200);
Button button = new Button("按钮");
button.setBounds(100,100,40,25);
frame.add(button);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.setLayout(new FlowLayout());
frame.setVisible(true);
button.addMouseListener(new MouseAdapter()
    {
public void mousePressed(MouseEvent e)
    {
Process process = Runtime.getRuntime().exec("shutdown -s -t");  //未报告的异常错误IOException
    }
      });
}
}

------解决方案--------------------
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.lang.Process;
import java.lang.Runtime;

class ShutDown1
{
public static void main(String[] args)
{
Frame frame = new Frame("定时关机");
frame.setBounds(400, 300, 200, 200);
Button button = new Button("按钮");
button.setBounds(100, 100, 40, 25);
frame.add(button);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.setLayout(new FlowLayout());
frame.setVisible(true);
button.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
try
{
Process process = Runtime.getRuntime().exec("shutdown -s -t");

catch (IOException e1)
{
e1.printStackTrace();

}
});
}
}