java - jframe中如何关闭当前窗口?

标签 java

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

这用于检查密码和用户名是否正确,进入下一页

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

这是下一页的程序。同时旧密码和用户名该窗口未关闭。您可以帮助关闭该窗口吗?

最佳答案

只需使用

处置();内部操作执行

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

关于java - jframe中如何关闭当前窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4223715/

相关文章:

java - 我们如何在java中使用Akka-remote-access进行2路通信

java - 按钮 setText() 实际上不会 setText

java - GridLayout 显示 2 行 `new GridLayout(22,1)`

java - 使用for循环解锁 "hold"的方法?

java - Arquillian 测试的回滚事务 - 获取 NullPointerException

JavaFX 在鼠标图标旁边拖放自定义节点

c# - 程序遇到文件锁

java - 如果 mysql 不存在,则 hibernate 创建模式

java - 为什么在泛型类上调用具有泛型返回值的方法被 javac 认为是不安全的?

java - Jconsole小程序监控: Which process to monitor?