2011-12-23 36 views
15

Tôi đã thử nghiệm và tìm kiếm và tôi dường như không thể hiểu được điều gì tôi nghĩ là đơn giản, có nút START của tôi tập trung khi ứng dụng GUI nhỏ của tôi khởi chạy Tức là, vì vậy tất cả người dùng phải làm là nhấn phím Enter/Return của họ, sẽ có tác dụng tương tự như thể họ đã nhấp vào nút BẮT ĐẦU bằng con chuột của họ. Đây là mã của tôi. Nhờ sự giúp đỡ của bạn :)Java GUI: Làm thế nào để tập trung vào JButton trong JPanel trên 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, cho anh ta là đủ duyên dáng để nói lời cảm ơn đến tất cả những ai đã trả lời câu trả lời. Chúc may mắn với công việc của bạn. Trân trọng –

Trả lời

26

Hãy thử mã này .. Tất cả tôi đã làm là di chuyển phương pháp requestFocus() ở cuối.

Về cơ bản đây là hai việc bạn phải làm để trả lời trong khi nhấn phím enter và để nó được tập trung theo mặc định.

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

Cảm ơn bạn đã trả lời :) – chrisco

+1

Tôi rất xin lỗi. Tôi mới ở đây và đã phạm sai lầm mới. Biết tôi biết. Một lần nữa, tôi rất xin lỗi. Lần tới tôi sẽ tìm câu trả lời đầu tiên và tốt nhất. – chrisco

+0

@chrisco: không có vấn đề gì và cảm ơn yoU! – bragboy

2

di chuyển dòng tập trung của bạn đến cuối của phương pháp

và thay đổi nó để

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

Cảm ơn bạn đã trả lời :) – chrisco

7

Nếu tôi hiểu bạn thì bạn muốn làm một sự kiện click của nút Start khi người dùng nhấn Enter key. Nếu đây là trường hợp sau đó bạn có thể làm điều đó như sau:

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

Và nếu bạn chỉ muốn nhấn mạnh vào vị bắt đầu nút sau đó chuyển phương pháp requestFocus() của bạn ở cuối (sau khi bạn thực hiện khung hình của bạn có thể nhìn thấy) và không cần để vượt qua true trong đó. Ngoài ra, tốt hơn nên sử dụng requestFocusInWindow() rồi requestFocus() như đã nêu trong tài liệu java.

+0

Hoạt động hoàn hảo, cảm ơn bạn rất nhiều! – chrisco

3

Nếu bạn muốn nút start của bạn để có được sự tập trung sau đó làm điều này vào cuối

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

Cảm ơn bạn đã trả lời :) – chrisco

+0

@chrisco Không có vấn đề :) – GETah

Các vấn đề liên quan