Java 如何关闭jframe中的当前窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4223715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close current window in jframe?
提问by Jomy Antony
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import class_program.NextPage;
class Login extends JFrame implements ActionListener
{
JButton SUBMIT;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;
Login()
{
label1 = new JLabel();
label1.setText("Username:");
text1 = new JTextField(15);
label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);
SUBMIT=new JButton("SUBMIT");
panel=new JPanel(new GridLayout(3,1));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(this);
setTitle("LOGIN FORM");
}
public void actionPerformed(ActionEvent ae)
{
String value1=text1.getText();
String value2=text2.getText();
if (value1.equals("jomy") && value2.equals("jomy")) {
NextPage page=new NextPage();
page.setVisible(true);
JLabel label = new JLabel("Welcome:"+value1);
page.getContentPane().add(label);
}
else{
System.out.println("enter the valid username and password");
JOptionPane.showMessageDialog(this,"Incorrect login or password",
"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
class jframes
{
public static void main(String arg[])
{
try
{
Login frame=new Login();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
}
}
this is used to check password and username that is correct going to next page
这用于检查下一页的正确密码和用户名
package class_program;
import javax.swing.JFrame;
public class NextPage extends JFrame
{
public NextPage()
{
setDefaultCloseOperation(javax.swing.
WindowConstants.DISPOSE_ON_CLOSE);
setTitle("");
setSize(400, 200);
}
}
this is program for next page .that the same time the old password and user name that window is not closed.can u helping for closing that window?
这是下一页的程序。同时窗口没有关闭的旧密码和用户名。你能帮忙关闭那个窗口吗?
回答by willcodejavaforfood
You close a JFrame by calling setVisible(false)
on it just like you open it by calling setVisible(true)
您可以通过调用setVisible(false)
来关闭 JFrame,就像通过调用打开它一样setVisible(true)
回答by lazydot
1) Your Login class doesnt define setDefaultCloseOperation(DO_SOMETHING_ON_CLOSE). 2) If you put setVisible(false); before
1) 您的登录类没有定义 setDefaultCloseOperation(DO_SOMETHING_ON_CLOSE)。2) 如果你把 setVisible(false); 前
nextPage page=new NextPage();
page.setVisible(true);
together with defining default close operation on the first frame it will work.
连同在第一帧上定义默认关闭操作,它将起作用。
回答by sunone5
just use the
只需使用
dispose(); inside actionPerformed
处置();内部行动执行
public void actionPerformed(ActionEvent ae)
{
String value1=text1.getText();
String value2=text2.getText();
if (value1.equals("jomy") && value2.equals("jomy")) {
dispose(); // this will close current login box window
//this will open a nextpage window.
NextPage page=new NextPage();
page.setVisible(true);
JLabel label = new JLabel("Welcome:"+value1);
page.getContentPane().add(label);
}
else{
System.out.println("enter the valid username and password");
JOptionPane.showMessageDialog(this,"Incorrect login or password",
"Error",JOptionPane.ERROR_MESSAGE);
}
回答by edilon Junior
In your new window method (newFrame) set this:
在您的新窗口方法 (newFrame) 中设置:
newFrame = new JFrame();
newFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);