重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java中窗体的关闭代码 关闭窗体的代码怎么写

JAVA如何用按钮关闭窗体

很久没有用过界面编程了,就当复习一下了,哈哈

创新互联服务项目包括曲沃网站建设、曲沃网站制作、曲沃网页制作以及曲沃网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,曲沃网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到曲沃省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

如一楼所说的,给按钮加一个监听器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();

}

}

java关闭窗体的六种方法

前段时间集中精力写了两篇论文 很久没写博文了 现在继续了

使用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中关闭当前窗口用什么代码

你用的 swing 吗?加上 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

或者加上窗口事件监听器:

addWindowListener(new WindowAdapter() {

public void windowClosing (WindowEvent we) {

dispose();

}

});

JAVA中怎么关闭一个窗口

java关闭窗口可以使用dispose(),只是该窗体在内存中所占有的资源得到了释放,而整个程序并没有因此而退出,如果整个程序要退出,在以java.awt中的frame为控件时,需手动添加:System.exit();

而在以javax.Swing中的jFrame为控件时,一般不需要再写相应的事件,默认点击窗体上的小叉时,是隐藏,当然你可更改其参数。

java程序关闭窗口代码

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);

}

}


当前题目:java中窗体的关闭代码 关闭窗体的代码怎么写
分享路径:http://cqcxhl.com/article/dochgse.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP