给Frame 添加一个监听就可以了,如果你是用的JFrame不用添加监听直接点击小叉叉就可以关闭窗口了
Frameframe=newFrame("测试");
frame.setSize(300,300);
frame.setVisible(true);
frame.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);//退出程序
}
});
❷ java的GUI编程,如何关闭一个窗体,而不是退出整个程序
JFrame的关闭可以使用这个方法进行关闭:setDefaultCloseOperation(int operation);
其中operation有以下几种
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关闭窗口代码
关闭代码:
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
清空输入栏
button.addActionListener(this);
再定义方法:
public void actionPerformed(ActionEvent e){
if(e.getSource()==button) //button为你的按钮名
{ text.setText(null); //text为你的输入栏名称
}
}
❹ 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如何用按钮关闭窗体
很久没有用过界面编程了,就当复习一下了,哈哈
如一楼所说的,给按钮加一个监听器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中关闭窗口的代码
想制作菜单里的退出效果吧?
这样吧,你给那个jMenuItem1_5添加一个ActionListener就行了,
jMenuItem1_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(..........)
else
System.exit(0);
}
});
这个可能有点危险,如果你的数据没有保存,或者是你要在后台处理一些东西的时候就会引起数据丢失;
不过你可以在里边在添加一个if else
来判断你的数据是否要保存或者后台的数据是不是已经完成,
如果没有完成要提醒用户,你可以使用JOptionPane里的警告对话框,来告诉用户!
❼ JAVA GUI swt,关闭当前窗口的代码(多个窗口的时候)
package tes;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AFrame extends JFrame implements ActionListener{
private JButton button;
private JPanel panel;
public void initFrame(String message,int x,int y,int z,int g){
setBounds(x,y,z,g);
setTitle(message);
button=new JButton("打开");
panel=new JPanel();
button.addActionListener(this);
panel.add(button);
add(panel);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(button)) {
new AFrame().initFrame("第二个窗口!",200,200,200,200);
}
}
public static void main(String[] args) {
new AFrame().initFrame("第一个窗口!",300,300,300,300);
}
}
代码虽然简单,但是可以回答你的问题了,自己运行一下试试。
❽ 如何在java程序中,当点击一个按钮后,关闭当前窗口,开启一个新的窗口。
JButtonbtn=newJButton(newAbstractAction("关闭并打开"){@(ActionEvente){oldFrame.dispose();//关闭并销毁,无需销毁可采用oldFrame.setVisible(false);newFrame.setVisible(true);//打开新窗口}});
❾ java如何关闭某JFrame窗口,而不是退出整个程序!
题主你addActionListener那段代码写错了。
正确代码:
register.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
newregister("新用户注册");//打开新的窗口
//这里版写关闭窗权口代码
dispose();
}
});