首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java GUI:如何在JFrame上的JPanel中设置对JButton的关注?

Java GUI:如何在JFrame上的JPanel中设置对JButton的关注?
EN

Stack Overflow用户
提问于 2011-12-23 20:14:53
回答 5查看 60.3K关注 0票数 19

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

代码语言:javascript
复制
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);

}
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-12-23 20:21:29

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

基本上,要让它在按enter键时响应,以及在默认情况下将其聚焦,您必须做这两件事。

代码语言:javascript
复制
frame.getRootPane().setDefaultButton(start);
start.requestFocus();

代码语言:javascript
复制
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();
    }
}
票数 30
EN

Stack Overflow用户

发布于 2011-12-23 20:23:01

如果我理解你,那么你想做一个点击事件的开始按钮,当用户点击回车键。如果是这样的话,你可以这样做:

代码语言:javascript
复制
jFrame.getRootPane().setDefaultButton(start);// 'start' will be your start button

如果你只是想在开始按钮上获得焦点,那么在最后(在你使你的框架可见之后)改变你的requestFocus()方法,而不需要在它里面传递true。而且,在java文档中,使用requestFocusInWindow()比使用requestFocus()更好。

票数 7
EN

Stack Overflow用户

发布于 2011-12-23 20:21:19

将焦点移动到方法的末尾

并将其更改为

代码语言:javascript
复制
start.requestFocus();  // without params
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8615958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档