How to close jframe from an actionPerformed?

Asked

Viewed 660 times

-1

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

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

1 answer

0


There are some errors in this code but I will focus only on those that are connected 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 are trying to access the screen with this. Obviously the compiler won’t even let you compile the code, static methods belong to the class, not the instance and the this is for reference the class object(instance) itself.

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

If necessary the Static, maybe you can apply the concept of Singleton, although it is impossible to say whether it is applicable to your case, as there is not enough information.

Reading recommends:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.