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

chr*_*sco 16 java user-interface swing

我已经进行了实验和搜索,我似乎无法弄清楚我认为什么是简单的东西,当我的小GUI应用程序启动时,我的START按钮具有焦点,所以用户所要做的就是按下他们的Enter /返回键,与使用鼠标单击"开始"按钮的效果相同.这是我的代码.谢谢你的帮助 :)

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

}
Run Code Online (Sandbox Code Playgroud)

bra*_*boy 27

试试这段代码..我所做的就是最后移动requestFocus()方法.

基本上这些是你必须做的两件事,它要在按下回车键时进行响应并默认聚焦.

frame.getRootPane().setDefaultButton(start);
start.requestFocus();
Run Code Online (Sandbox Code Playgroud)

图像的屏幕截图

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();
    }
}
Run Code Online (Sandbox Code Playgroud)


Har*_*Joy 7

如果我理解你,那么当用户点击Enter键时你想要点击开始按钮事件.如果是这种情况,那么您可以按如下方式进行:

jFrame.getRootPane().setDefaultButton(start);// 'start' will be your start button
Run Code Online (Sandbox Code Playgroud)

如果你只是想把重点放在开始按钮上,那么最后移动你的requestFocus()方法(在你的框架可见之后)并且不需要传递true它.此外,最好按照java doc中的说明使用requestFocusInWindow()then requestFocus().