gpt4 book ai didi

Java 图形用户界面 : How to Set Focus on JButton in JPanel on JFrame?

转载 作者:太空狗 更新时间:2023-10-29 22:42:40 24 4
gpt4 key购买 nike

我已经进行了试验和搜索,但我似乎无法弄清楚我认为什么是简单的,即当我的小 GUI 应用程序启动时让我的开始按钮获得焦点,即,所以用户所要做的就是按下他们的 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);

}

最佳答案

尝试这段代码。我所做的只是将 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();
}
}

关于Java 图形用户界面 : How to Set Focus on JButton in JPanel on JFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8615958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com