He experimentado y buscado y parece que no puedo entender lo que pensé que sería algo simple, que es tener mi botón START enfocado cuando mi pequeña aplicación GUI se ejecuta, es decir, todo lo que el usuario tiene que hacer es presionar su tecla Enter/Return, que tendrá el mismo efecto que si hubieran presionado el botón START con su mouse. Aquí está mi código. Gracias por su ayuda :)Java GUI: ¿Cómo establecer el foco en JButton en JPanel en JFrame?
private void initialize() {
// Launch the frame:
frame = new JFrame();
frame.setTitle("Welcome!");
frame.setSize(520, 480);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add the image:
ImageIcon heroShotImage = new ImageIcon("heroShot.jpg");
JPanel heroShotPanel = new JPanel();
JLabel heroShot = new JLabel(heroShotImage);
heroShotPanel.add(heroShot);
// Create a panel to hold the "Start" button:
JPanel submitPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
// Create the "Start" button, which launches business logic and dialogs:
JButton start = new JButton("Start");
start.setToolTipText("Click to use library");
start.setFocusable(true); // How do I get focus on button on App launch?
start.requestFocus(true); // Tried a few things and can't get it to work.
// Listen for user actions and do some basic validation:
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// THE APP's LOGIC GOES HERE...
}
// Finish setting up the GUI and its components, listeners, and actions:
submitPanel.add(start);
frame.getContentPane().add(heroShotPanel, BorderLayout.NORTH);
frame.getContentPane().add(submitPanel, BorderLayout.SOUTH);
}
1, para él es lo suficientemente amable para decir gracias a todos los que respondieron con una respuesta. Buena suerte con tu trabajo. Saludos –