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();
}
}
});
}
}
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();
}
}
});
}
}
try
{
Process process = Runtime.getRuntime().exec("shutdown -s -t"); //这条语句的Process process是什么
}
catch (IOException e1) //请问为什么加了try{} catch{}语句块后就编译通过了 , 这该怎么理解?请指教!谢谢了!
e1.printStackTrace();
}
当在程序中调用另一个可执行程序或系统命令时,可以用Java的Runtime类和Process类来实现;
process是调用系统的shutdown命令;
程序涉及到输入输出以及系统命令时,都会抛出一个IO异常,这时要么捕获处理,要么继续往上抛出这个异常;