java程序启动之后,JVM(java虚拟机)会实例化一个Runtime类,这是一个运行时类实例,包含这个java程序运行的所有信息,一个程序会实例化一个Runtime类且只能实例化一个Runtime类。我们可以通过这个Runtime类实例操作程序的运行。它的exit(int status)方法会终止程序的运行。例:
Runtime.getRuntime().exit(0)
其中参数status表示状态,0表示正常退出,非0表示异常退出。非0参数有什么用呢?
addShutdownHook(Thread hook)方法可以将一个Thread可程序的关闭hook(挂/联系)起来,程序退出前,会执行这个方法,这样我们可以根据退出状态作相应的善后操作。
另外,System.exit(int status)也是调用这个Runtime类实例的退出方法来间接实现程序的退出的。
再加一点,Runtime类可以创建子进程,如下:
Process p=Runtime.getRuntime().exec("notepad")
这个语句将会创建一个记事本程序(它是Runtime类创建的子进程)
p.destroy()
这个语句终止刚才创建的子进程。
前段时间集中精力写了两篇论文 很久没写博文了 现在继续了
使用JFrame的enableEvents和processWindowEvent
//Frame java
import java awt *
import java awt event *
import javax swing *
public class Frame extends JFrame {
public Frame () {
enableEvents(AWTEvent WINDOW_EVENT_MASK)
this setSize(new Dimension( ))
this setTitle( Frame )
}
protected void processWindowEvent(WindowEvent e) {
super processWindowEvent(e)
if (e getID() == WindowEvent WINDOW_CLOSING) {
System exit( )
}
}
}
直接实现WindowListener接口
//Frame java
import java awt *
import java awt event *
public class Frame extends Frame implements WindowListener {
public Frame () {
this setSize(new Dimension( ))
this setTitle( Frame )
this addWindowListener(this)
}
public void windowClosing(WindowEvent windowEvent) {
System exit( )
}
public void windowOpened(WindowEvent windowEvent) { }
public void windowClosed(WindowEvent windowEvent) { }
public void windowIconified(WindowEvent windowEvent) { }
public void windowDeiconified(WindowEvent windowEvent) { }
public void windowActivated(WindowEvent windowEvent) { }
public void windowDeactivated(WindowEvent windowEvent) { }
}
直接继承窗体适配器WindowAdapter
//Frame java
import java awt *
import java awt event *
public class Frame extends WindowAdapter {
public Frame () {
Frame f=new Frame()
f setSize(new Dimension( ))
f setTitle( Frame )
f addWindowListener(this)
f setVisible(true)
}
public static void main(String[] s){
new Frame ()
}
public void windowClosing(WindowEvent windowEvent) {
System exit( )
}
}
间接继承窗体适配器WindowAdapter
//Frame java
import java awt *
import java awt event *
public class Frame extends Frame {
public Frame () {
this setSize(new Dimension( ))
this setTitle( Frame )
this addWindowListener(new winAdapter())
this setVisible(true)
}
public static void main(String[] s){
new Frame ()
}
}
class winAdapter extends WindowAdapter{
public void windowClosing(WindowEvent windowEvent) {
System exit( )
}
}
间接实现WindowListener接口
//Frame java
import java awt *
import java awt event *
public class Frame extends Frame {
public Frame () {
this setSize(new Dimension( ))
this setTitle( Frame )
this addWindowListener(new winEventHandle())
this setVisible(true)
}
public static void main(String[] s){
new Frame ()
}
}
class winEventHandle implements WindowListener {
public void windowClosing(WindowEvent windowEvent) {
System exit( )
}
public void windowOpened(WindowEvent windowEvent) { }
public void windowClosed(WindowEvent windowEvent) { }
public void windowIconified(WindowEvent windowEvent) { }
public void windowDeiconified(WindowEvent windowEvent) { }
public void windowActivated(WindowEvent windowEvent) { }
public void windowDeactivated(WindowEvent windowEvent) { }
}
使用Inner Class
//Frame java
import java awt *
import java awt event *
public class Frame {
public Frame (){
Frame f=new Frame()
f addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System exit( )
}
})
f setSize(new Dimension( ))
f setVisible(true)
}
public static void main(String[] s){
new Frame ()
}
}
Jframe的关闭方法
setDefaultCloseOperation(EXIT_ON_CLOSE)
frame的关闭方法如下
this addWindowListener(new java awt event WindowAdapter() {
public void windowClosing(java awt event WindowEvent e) {
System exit( )
}
lishixinzhi/Article/program/Java/hx/201311/27073