Hi friends.
How can I do ActionListener to a button with pressing the Enter key in keyboard?
My code is this:
import javax.swing.*; import java.awt.*; public class myclass{ public static void main(String args[]){ JFrame window = new JFrame(); JTextField username, password; JLabel usernamelabel, passwordlabel, sentence; JButton button; window.setSize(400, 200); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setBackground(Color.white); window.setResizable(false); window.setLayout(null); window.setTitle("Wellcome."); usernamelabel = new JLabel("Username: "); usernamelabel.setBounds(100, 40, 80, 30); window.add(usernamelabel); username = new JTextField(""); username.setBounds(180, 40, 120, 30); window.add(username); passwordlabel = new JLabel("Password: "); passwordlabel.setBounds(100, 80, 80, 30); window.add(passwordlabel); password = new JTextField(""); password.setBounds(180, 80, 120, 30); window.add(password); button = new JButton(" OK "); button.setBounds(160, 120, 80, 30); window.add(button); sentence = new JLabel("Please enter your username and password"); sentence.setBounds(65, 8, 270, 30); sentence.setForeground(Color.DARK_GRAY); window.add(sentence); window.setVisible(true); } }