重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
很久没有用过界面编程了,就当复习一下了,哈哈
我们提供的服务有:做网站、成都网站设计、微信公众号开发、网站优化、网站认证、长顺ssl等。为成百上千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的长顺网站制作公司
如一楼所说的,给按钮加一个监听器ActionListener,写一个实现方法
actionPerformed.此时当按钮点击时会调用actionPerformed方法,代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Close extends JFrame implements ActionListener{
JButton close;
public Close(){
close = new JButton("close");//增加一个按钮
add(close);
close.addActionListener(this);//给按钮增加一个监听器
setLayout(new FlowLayout());
setSize(200,100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//捕捉到按钮点击时的事件处理方法
//按钮点击时一定会自动执行actionPerformed(ActionEvent e)方法
public void actionPerformed(ActionEvent e){
//关闭整个应用程序.如果只是是想关闭当前窗口,可以用
//dispose();
System.exit(0);
}
public static void main(String[] args){
new Close();
}
}
import java.applet.*;
import java.awt.Color;
import java.awt.Frame;
import javax.swing.JFrame;
import java.awt.event.*;
public class FirstFrame extends Frame {
public static void main(String args[]) {
FirstFrame fr = new FirstFrame("First contianer!");
fr.setSize(240, 240);
//继承JFrame的关闭窗口代码
//fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//继承Frame的
fr.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);//退出系统
}
});
fr.setVisible(true);
}
public FirstFrame(String str) {
super(str);
}
}
前段时间集中精力写了两篇论文 很久没写博文了 现在继续了
使用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
效果图
你想要触发的是窗口关闭事件windowClosed, 那么你需要调用dispose方法,对窗口进行销毁.
修改代码如下
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing--窗体正在关闭事件");
//f.setVisible(false);//隐藏窗口
f.dispose();//销毁窗口--这行代码会触发windowClosed事件
}
还有其他一些细节的修改,参考下面的代码
import java.awt.*;
import java.awt.event.*;
//类名不要写一个小写字母,推荐写一个完整的名词来作为类名
public class EventDemo {
public static void main(String[] args) {
Frame f = new Frame("hello");
f.setSize(300, 300);
f.setLocation(300, 200);
//f.setVisible(true);//不要先显示窗口, 先显示了窗口,就看不到"窗口打开事件"了
f.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened--窗体打开事件");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("windowIconified--窗体图标化事件");
}
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("windowDeiconifie--窗体取消图标化事件");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("windowDeactivated--窗体停用事件");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing--窗体正在关闭事件");
//f.setVisible(false);//隐藏窗口
f.dispose();//销毁窗口
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("windowClosed--窗体关闭事件");
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated--窗体激活事件");
}
});
f.setVisible(true);//窗口显示
}
}