How to close jframe from an actionPerformed? [duplicate]

-1

I created a login java application, but I can not close the screen during ActionPerformed of the button with this.dispose();

What do I do to make it work?

Code:

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.*;

public class Login extends JFrame {

    /**
     * @author Wanghley
     */
    private static final long serialVersionUID = 1L;
    private JPasswordField passwordField;
    private JTextField txtUser;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public static String user, password;
    public Login() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 345, 207);
        getContentPane().setLayout(null);

        passwordField = new JPasswordField();
        passwordField.setBounds(78, 80, 230, 20);
        getContentPane().add(passwordField);

        txtUser = new JTextField();
        txtUser.setBounds(78, 36, 230, 20);
        getContentPane().add(txtUser);
        txtUser.setColumns(10);

        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt) {
                String users = txtUser.getText();
                char[] pass = passwordField.getPassword();
                String passw = null;
                for (int i = 0; i < pass.length; i++) {
                    passw = pass[i]+"";
                }
                Login(evt);
            }
        });
        btnLogin.setBounds(123, 121, 106, 37);
        getContentPane().add(btnLogin);

        JLabel lblPassword = new JLabel("Password:");
        lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
        lblPassword.setBounds(10, 83, 58, 14);
        getContentPane().add(lblPassword);

        JLabel lblUser = new JLabel("User:");
        lblUser.setHorizontalAlignment(SwingConstants.RIGHT);
        lblUser.setBounds(10, 39, 58, 14);
        getContentPane().add(lblUser);

        JLabel lblLoginAdmin = new JLabel("Login Admin");
        lblLoginAdmin.setHorizontalAlignment(SwingConstants.CENTER);
        lblLoginAdmin.setFont(new Font("Liberation Sans", Font.BOLD, 14));
        lblLoginAdmin.setBounds(10, 0, 319, 25);
        getContentPane().add(lblLoginAdmin);
    }
    private static void Login(ActionEvent evt){
        //não consigo fechar a tela com o this.dispose();
        this.dispose();
    }
}
    
asked by anonymous 23.11.2018 / 19:49

1 answer

0

There are some errors in this code but I will focus only on those that are linked to the question problem.

In addition to naming the method incorrectly giving the same class name, without following the java conventions , you declared the method as static and is trying to access the screen with this . Obviously the compiler will neither let code compile, static methods belong to the class, not the instance, and the this is for referencing the itself object (instance) of class .

The simplest solution is to close directly in the action, since this method only exists to close the window in any sense, or at least it was not explained in the question. Within the actionListener, instead of Login(evt); , call directly dispose() .

If static is required, you might be able to apply the singleton concept although it is impossible to say if it is applicable to your case, as there is not enough information.

Reading recommends:

23.11.2018 / 22:34