网站建设资讯

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程序关闭窗口代码

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://cdweb.net/article/doddhed.html