1
I have a login screen to a system and I want that when the user has a selected button, when pressing "Enter" on the keyboard, the button triggers its action. Ex.:
- if the confirm button is selected and enter is activated log in
- if the cancel button is selected and enter is activated close the screen
Follow the code of the ready screen:
public class loginPopup implements ActionListener
{
private JFrame ourFrame = new JFrame("Login");
JTextField user_text = new JTextField();
JPasswordField pass_text = new JPasswordField();
JDateChooser calendario = new JDateChooser();
JButton yesButton = new JButton("Confirmar");
JButton noButton = new JButton("Cancelar");
public loginPopup()
{
ourFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ourFrame.setBounds(600, 300, 400, 250);
Container container = ourFrame.getContentPane();
container.setLayout(null);
JLabel logo = new JLabel("INSIRA SEUS DADOS DO SISTEMA");
logo.setBounds(105, 5, 250, 30);
JLabel user_label = new JLabel("Usuário:");
user_label.setBounds(20, 40, 250, 30);
JLabel pass_label = new JLabel("Senha:");
pass_label.setBounds(20, 80, 250, 30);
user_text.setBounds(70, 45, 250, 20);
pass_text.setBounds(70, 85, 250, 20);
calendario.setBounds(90, 120, 210, 25);
yesButton.setBounds(70, 165, 100, 30);
yesButton.addActionListener(this);
noButton.setBounds(210, 165, 100, 30);
noButton.addActionListener(this);
container.add(logo);
container.add(user_label);
container.add(pass_label);
container.add(user_text);
container.add(pass_text);
container.add(calendario);
container.add(yesButton);
container.add(noButton);
ourFrame.setVisible(true);
}
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == yesButton)
{
String user = user_text.getText();
String pass = pass_text.getText();
Date oldDate = calendario.getDate();
//CODIGO DA AÇÃO EX.:
System.out.println("Confirmar!");
}
else if (e.getSource() == noButton)
{
System.exit(0);
}
}
public static void main(String[] args)
{
new loginPopup();
}
}
Now I need to add the "Enter" part but I can’t do it