java关闭键代码 java屏蔽代码快捷键

java设置关闭窗口按钮

package org.t20110219.test;

创新互联IDC提供业务:服务器托管,成都服务器租用,服务器托管,重庆服务器租用等四川省内主机托管与主机租用业务;数据中心含:双线机房,BGP机房,电信机房,移动机房,联通机房。

/**

* 修改后的代码

* 能关闭,

* 希望能帮助你

*/

import java.awt.*;

import java.awt.event.*;

public class MainProgram extends Frame implements ActionListener,WindowListener

{

int i;

Button b1 = new Button("确定");

Button b2 = new Button("关闭");

void FlowLayoutEX()

{

this.setTitle("布局管理器");

this.setLayout(new FlowLayout());

this.add(b1);

b1.addActionListener(this);

this.add(b2);

b2.addActionListener(this);

this.addWindowListener(this);

this.setBounds(100, 100, 250, 250);

this.setVisible(true);

}

public void actionPerformed(ActionEvent e)

{

/**------------修改代码--------------*/

/**

* 修改 合理就可以了

* e.getSource() 可以得到 你点击了哪个 按钮

* 判断之后就可以 设定 对应的 操作了

*/

if(e.getSource()==b2){

System.exit(0);

}else{

i++;

Button bi = new Button("按钮"+i);

this.add(bi);

this.show(true);

}

}

public void windowClosing(WindowEvent f)

{

System.exit(0);

}

public void windowOpened(WindowEvent f){}

public void windowClosed(WindowEvent f){}

public void windowIconified(WindowEvent f){}

public void windowDeiconified(WindowEvent f){}

public void windowActivated(WindowEvent f){}

public void windowDeactivated(WindowEvent f){}

public static void main(String args[])

{

MainProgram window = new MainProgram();

window.FlowLayoutEX();

}

}

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

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

}

}

网页标题:java关闭键代码 java屏蔽代码快捷键
网站链接:https://www.cdcxhl.com/article28/hhjdcp.html

成都网站建设公司_创新互联,为您提供品牌网站设计网站导航营销型网站建设搜索引擎优化网站设计手机网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

商城网站建设