App.java
import javax.swing.SwingUtilities;
public class App {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
LoginForm loginForm = new LoginForm();
loginForm.setVisible(true);
});
}
}
LoginForm.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginForm extends JFrame {
private JTextField textFieldUsername;
private JPasswordField passwordFieldPassword;
private JButton buttonLogin;
private JButton buttonExit;
public LoginForm() {
setTitle("TEPOO - Login");
setSize(300, 160);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
textFieldUsername = new JTextField();
passwordFieldPassword = new JPasswordField();
buttonLogin = new JButton("Login");
buttonExit = new JButton("Sair");
JPanel innerPanel = new JPanel(new GridLayout(3, 1));
innerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
innerPanel.add(new JLabel("Usuário:"));
innerPanel.add(textFieldUsername);
innerPanel.add(new JLabel("Senha:"));
innerPanel.add(passwordFieldPassword);
innerPanel.add(buttonLogin);
innerPanel.add(buttonExit);
JLabel labelTitle = new JLabel("Entrar:");
labelTitle.setAlignmentX(JLabel.CENTER_ALIGNMENT);
JPanel outterPanel = new JPanel();
outterPanel.setLayout(new BoxLayout(outterPanel, BoxLayout.Y_AXIS));
outterPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
outterPanel.add(labelTitle);
outterPanel.add(innerPanel);
this.add(outterPanel);
buttonLogin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = textFieldUsername.getText();
String password = String.valueOf(passwordFieldPassword.getPassword());
if (username.equals("Admin") && password.equals("1234")) {
JOptionPane.showMessageDialog(null, "Bem-vindo, Admin!", null, JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Usuário e/ou senha inválidos.", "Erro",
JOptionPane.ERROR_MESSAGE);
}
}
});
buttonExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}