Ho sperimentato e cercato e non riesco a capire quello che pensavo sarebbe stato qualcosa di semplice, che sta avendo il mio pulsante START focalizzato quando la mia piccola app GUI lancia Ie, quindi tutto quello che l'utente deve fare è premere il loro tasto Invio/Invio, che avrà lo stesso effetto come se avessero fatto clic sul pulsante START con il mouse. Ecco il mio codice. Grazie per il vostro aiuto :)GUI Java: Come impostare Focus su JButton in JPanel su 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, per lui di essere così gentile per dire grazie a tutti coloro che hanno risposto con una risposta. Buona fortuna con il tuo lavoro. Cordiali saluti –