2011-12-23 44 views
15

我已经试验和搜索,我似乎无法弄清楚我认为会是简单的事情,当我的小GUI应用程序启动时,我的START按钮有焦点,所以用户所要做的就是按下他们的Enter/Return键,它们的效果就好像他们用鼠标点击了START按钮一样。这是我的代码。感谢您的帮助:)Java GUI:如何在JFrame上的JPanel中将焦点设置在JButton上?

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,对他来说是很慷慨对所有回答了答案的人表示感谢。祝你工作顺利。 Regards –

回答

26

尝试这个代码..我所做的是移动最后的requestFocus()方法。

基本上,这些是你必须做的两件事情,让它按回车键进行响应,并默认情况下它会被聚焦。

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

谢谢你的回复:) – chrisco

+1

我很抱歉。我是新来的,犯了一个新手错误。知道我知道。再次,我非常抱歉。下次我会寻找第一个和最好的答案。 – chrisco

+0

@chrisco:没问题,谢谢你! – bragboy

2

你的重点线移到方法

的末端,将其更改为

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

谢谢你的回复:) – chrisco

7

如果我理解你,那么你想要做的开始按钮的点击事件,当用户点击Enter键。如果是这种情况,那么你可以如下做到这一点:

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

如果你只是想专注于开始按钮,然后在最后改变你的requestFocus()方法(你让你的框架可见后),没有必要在其中通过true。如java文档中所述,最好使用requestFocusInWindow(),然后requestFocus()

+0

完美的作品,非常感谢! – chrisco

3

如果你希望你的start按钮获取焦点,然后做到这一点,在里面一个容器结束

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

谢谢你的回复:) – chrisco

+0

@chrisco没问题:) – GETah

相关问题