【问题标题】:Java GUI: How to Set Focus on JButton in JPanel on JFrame?Java GUI:如何在 JFrame 上的 JPanel 中将焦点设置在 JButton 上?
【发布时间】:2011-12-23 12:14:53
【问题描述】:

我已经进行了实验和搜索,但我似乎无法弄清楚我认为什么是简单的事情,即当我的小 GUI 应用程序启动时让我的 START 按钮具有焦点,所以用户所要做的就是按他们的 Enter/Return 键,其效果与他们用鼠标单击 START 按钮的效果相同。这是我的代码。感谢您的帮助:)

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,因为他足够亲切地向所有回答的人表示感谢。祝你工作顺利。问候

标签: java swing user-interface


【解决方案1】:

试试这段代码。我所做的只是把requestFocus()方法移到最后。

基本上,这是您必须做的两件事,它才能在按下回车键时做出响应,并使其在默认情况下获得焦点。

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

【讨论】:

  • 非常抱歉。我是新来的,犯了一个新手错误。知道我知道。再次,我非常抱歉。下次我会寻找第一个也是最好的答案。
【解决方案2】:

将焦点线移到方法的末尾

并将其更改为

start.requestFocus();  // without params

【讨论】:

    【解决方案3】:

    不容易,因为 Focus/Focus_SubSystem 来自 Native OS 并且非常异步,

    1) 在一个容器内通过将其包装到 invokeLater() 中来工作,

    2) manage Focus betweens two or more Top-Level Containers,作者 @camickr

    【讨论】:

      【解决方案4】:

      如果我理解你的意思,那么你想在用户按下 Enter 键时执行 Start Button 的单击事件。如果是这种情况,那么你可以这样做:

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

      如果您只想专注于开始按钮,那么在最后(在您使框架可见之后)转移您的 requestFocus() 方法,无需在其中传递 true。此外,最好使用requestFocusInWindow(),然后使用requestFocus(),如java doc中所述。

      【讨论】:

        【解决方案5】:

        如果您希望 start 按钮获得焦点,请在最后执行此操作

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-09
          • 1970-01-01
          • 2016-12-06
          • 1970-01-01
          • 1970-01-01
          • 2013-01-17
          相关资源
          最近更新 更多