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