前段时间集中精力写了两篇论文 很久没写博文了 现在继续了
使用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在Java的多线程编程中,java.lang.Thread类型包含了一些列的方法start(), stop(), stop(Throwable) and suspend(), destroy() and resume()。通过这些方法,我们可以对线程进行方便的操作,但是这些方法中,只有start()方法得到了保留。\x0d\x0a在Sun公司的一篇文章《Why are Thread.stop, Thread.suspend and Thread.resume Deprecated? 》中详细讲解了舍弃这些方法的原因。\x0d\x0a如果真的需要终止一个线程,可以使用以下几种方法: \x0d\x0a1、让线程的run()方法执行完,线程自然结束。(这种方法最好)\x0d\x0a\x0d\x0a2、通过轮询和共享标志位的方法来结束线程,例如while(flag){},flag的初始值设为真,当需要结束时,将flag的值设为false。(这种方法也不很好,因为如果while(flag){}方法阻塞了,则flag会失效)\x0d\x0a如果线程因为执行sleep()或是wait()而进入Not Runnable状态,假如是wait() 用标志位就方法就不行了,\x0d\x0apublic final void wait(long timeout)\x0d\x0athrows InterruptedException此方法导致当前线程(称之为 T)将其自身放置在对象的等待集中,然后放弃此对象上的所有同步要求。即当前线程变为等待状态\x0d\x0await() 的标准使用方法\x0d\x0asynchronized(obj){\x0d\x0awhile(){\x0d\x0aobj.wait()\x0d\x0a}\x0d\x0a满足条件的处理过程\x0d\x0a}\x0d\x0a而您想要停止它,您可以使用第三种即\x0d\x0a3 使用interrupt(),而程式会丢出InterruptedException例外,因而使得执行绪离开run()方法执行shutdown就可以关闭。Java语言是一种计算机语言,尤用于创建网站Java是一门面向对象的编程语言。根据Java官网资料显示,java执行shutdown就可以关闭。吸收了优点,还摒弃了概念,给人们的使用带来便捷,更好的服务人们。