网站建设资讯

NEWS

网站建设资讯

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

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

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

addWindowListener(new WindowAdapter() {

public void windowClosing (WindowEvent we) {

dispose();

}

});

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 永远关不掉的窗口代码
URL分享:http://cdweb.net/article/dodspgg.html