简体   繁体   English

Java GUI:如何在JFrame上的JPanel中设置JButton的焦点?

[英]Java GUI: How to Set Focus on JButton in JPanel on JFrame?

I've experimented and searched and I can't seem to figure out what I thought would be something simple, which is having my START button have focus when my little GUI app launches Ie, so all the user has to do is press their Enter/Return key, which will have the same effect as if they had clicked the START button with their mouse. 我已经进行了实验和搜索,我似乎无法弄清楚我认为什么是简单的东西,当我的小GUI应用程序启动时,我的START按钮具有焦点,所以用户所要做的就是按下他们的Enter /返回键,与使用鼠标单击“开始”按钮的效果相同。 Here is my code. 这是我的代码。 Thanks for your help :) 谢谢你的帮助 :)

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

}

Try out this code.. All I have done is moving the requestFocus() method at the end. 试试这段代码..我所做的就是最后移动requestFocus()方法。

Basically these are the two things you have to do for it to respond while pressing enter key and for it to be focused by default. 基本上这些是你必须做的两件事,它要在按下回车键时进行响应并默认聚焦。

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

图像的屏幕截图

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

If I'm understanding you then you want to do a click event of Start Button when user hits Enter key. 如果我理解你,那么当用户点击Enter键时你想要点击开始按钮事件。 If this is the case then you can do it as follows: 如果是这种情况,那么您可以按如下方式进行:

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

And if you just want to get focus on start Button then shift your requestFocus() method at the end (after you make your frame visible) and no need to pass true in it. 如果你只想把注意力集中在开始Button上,那么最后将你的requestFocus()方法移动(在你的框架可见之后)并且不需要在其中传递true Also it is better to use requestFocusInWindow() then requestFocus() as stated in java doc. 另外最好使用requestFocusInWindow()然后使用requestFocus()如java doc中所述。

move your focus line to the end of the method 将焦点线移动到方法的末尾

and change it to 并将其更改为

start.requestFocus();  // without params

If you want your start button to get the focus then do this at the end 如果您希望您的start按钮获得焦点,请在最后执行此操作

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

not easy job because Focus/Focus_SubSystem came from Native OS and is pretty asynchronous, 不容易的工作,因为Focus / Focus_SubSystem来自Native OS并且非常异步,

1) inside one Containers works by wraping that into invokeLater(), 1)在一个容器内部通过将其包装到invokeLater()中来工作,

2) manage Focus betweens two or more Top-Level Containers , by @camickr 2)由@camickr 管理两个或多个顶级容器之间的聚焦

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM