前段时间集中精力写了两篇论文 很久没写博文了 现在继续了
使用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方法一:
类 JFrame
javax.swing.JFrame
JFrame中的方法void setDefaultCloseOperation(int)可以设置
以下为改方法的用法:
setDefaultCloseOperation
public void setDefaultCloseOperation(int operation)设置用户在此窗体上发起
"close" 时默认执行的 *** 作。必须脊销指定以下选项之一:
DO_NOTHING_ON_CLOSE(在 WindowConstants 中定义):不执行任何 *** 作;要求程序在已注册的
WindowListener 对象的 windowClosing 方法中处理该 *** 作。
HIDE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册的 WindowListener
对象后自动隐藏该窗体。
DISPOSE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册 WindowListener
的对象后自动隐藏并释放该窗体。
EXIT_ON_CLOSE(在 JFrame 中定义):使用 System exit
方法退出应用程序。仅在应用程序中使用。
默认情况下,该值被设置为 HIDE_ON_CLOSE。更改此属性的值将导致激发属性更改事件,其属性名称为
"defaultCloseOperation"。
注:当 Java 虚拟机 (VM) 中最衫野庆后一个可显示窗口被释放后,虚拟机可能会终止
要实现你说的,应该采用
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
方法二:
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JFrame
import javax.swing.JOptionPane
public class Test extends JFrame {
public Test(){
this.setTitle("title")
this.setSize(300,200)
this.setLocation(100,100)
//设置关闭时什么也不做
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE)
//监听关闭按钮的点击 *** 作
this.addWindowListener(new WindowAdapter(){
//new 一个WindowAdapter 类 重写windowClosing方法
//WindowAdapter是个适配器类 具体看jdk的帮助文或握档
public void windowClosing(WindowEvent e) {
//这里写对话框
if(JOptionPane.showConfirmDialog(null,
"退出","提
示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){
System.exit(0)
}
}
})
this.setVisible(true)
}
public static void main(String[] args) {
new Test()
}
}
效果图
参考代码和注释如下
import java.awt.event.*import javax.swing.*
public class DemoFrame extends JFrame{
JButton jbExit
public DemoFrame() {
jbExit = new JButton("退出")
//当点击退出 按钮时候的响应器
jbExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doExit()//退出时候的方法睁郑
}
})
JPanel jp = new JPanel()
jp.add(jbExit)
add(jp)
setTitle("窗口")// 窗口标题
setSize(380, 185)// 窗口大小
setLocationRelativeTo(null)// 窗口居中
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//通常添加这行代码,点击窗口右下角的关闭时会结束程序
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)//右下角的关闭,不主动采取任何行动
//当点击窗口右上角的关闭按钮时候的响应悉洞颂器
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
doExit()
}
})
}
// main方法
public static void main(String[] 颤扮args) {
new DemoFrame().setVisible(true)
}
//退出时候的选择
private void doExit() {
int n = JOptionPane.showConfirmDialog(null, "你确定要退出吗?", "消息提示",JOptionPane.YES_NO_OPTION)
//取消选择是 -1 ,确定是0 ,取消是1
System.out.println(n)
if(n==0) { //如果选择了确定
System.exit(0)//那么退出
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)