2011-12-23 6 views
15

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); 

} 
+0

+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 –

risposta

26

Prova questo codice .. Tutto quello che ho fatto è in movimento il metodo requestFocus() alla fine.

Fondamentalmente queste sono le due cose che devi fare perché risponda premendo il tasto Invio e per essere messo a fuoco di default.

frame.getRootPane().setDefaultButton(start); 
start.requestFocus(); 

Screenshot of the image

package sof; 

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class TestFrame { 

    public static void main(String[] args) { 
     // Launch the frame: 
     JFrame 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)); 

     JButton start = new JButton("Start"); 
     start.setToolTipText("Click to use library"); 

     start.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.out.println("I AM PRESSED"); 
      } 
     }); 

     submitPanel.add(start); 

     frame.getContentPane().add(heroShotPanel, BorderLayout.NORTH); 
     frame.getContentPane().add(submitPanel, BorderLayout.SOUTH); 
     frame.setVisible(true); 
     frame.getRootPane().setDefaultButton(start); 
     start.requestFocus(); 
    } 
} 
+0

Grazie per la risposta :) – chrisco

+1

Mi dispiace molto. Sono nuovo qui e ho fatto un errore da principiante. So che lo so. Di nuovo, mi dispiace molto. La prossima volta cercherò la PRIMA E MIGLIORE risposta. – chrisco

+0

@chrisco: nessun problema e grazie! – bragboy

2

spostare la linea di attenzione alla fine del metodo

e modificarlo in

start.requestFocus(); // without params 
+0

Grazie per la risposta :) – chrisco

7

Se ti sto capendo poi si vuole fare un evento click del pulsante di avvio quando utente preme il tasto Invio. Se questo è il caso, allora si può fare come segue:

jFrame.getRootPane().setDefaultButton(start);// 'start' will be your start button 

E se si desidera solo per ottenere attenzione per pulsante Start quindi spostare il metodo di requestFocus() alla fine (dopo aver effettuato la cornice a vista) e non c'è bisogno per passare true in esso. Inoltre è meglio usare requestFocusInWindow() quindi requestFocus() come indicato in java doc.

+0

Funziona perfettamente, grazie mille! – chrisco

3

Se si desidera che il pulsante start per ottenere la messa a fuoco poi fare questo alla fine

//This button will have the initial focus. 
start.requestFocusInWindow(); 
+1

Grazie per la risposta :) – chrisco

+0

@chrisco Nessun problema :) – GETah

Problemi correlati