java swing close window without exiting app

后端 未结 8 1554
慢半拍i
慢半拍i 2021-01-12 04:31

I have a little frame where I ask user & password. This frame will be opened clicking over a button in a main window.

Then I have two buttons: ok and cancel.

8条回答
  •  死守一世寂寞
    2021-01-12 05:27

    Use this.dispose(); in the action listener method when the username/password succeeds. eg:

    public void actionPerformed(ActionEvent ae) {
    
        if(ae.getSource()=="button you press to confirm username/password"){
            if(userNameTf.getText().equals(username)&&isPassword(passwordTf.getPassword())){
                new "window to be opened upon success"
                this.dispose(); // calls dispose on this object ie. 
                                                // the login window 
            }
            else{
                userNameTf.setText("");
                passwordTf.setText("");
    
                JOptionPane.showMessageDialog(this,
                        "Username and/or password is incorrect!",
                        "Attention!",
                        JOptionPane.WARNING_MESSAGE);
            }
        }
    
    }
    

    If you are using inner classes to handle the events just replace 'this.dispose()' with Super_Class_Name.this.dispose();

提交回复
热议问题