【问题标题】:JTextArea and JButton. Set the focus on the button [duplicate]JTextArea 和 JButton。将焦点设置在按钮上[重复]
【发布时间】:2018-10-25 08:52:53
【问题描述】:

以下程序显示JTextArea 和“关闭”JButton。我将按钮定义为默认按钮,因为我希望它在打开窗口后立即获得焦点。但是,当我运行程序时,按钮会突出显示,但焦点在文本字段上。

如何将焦点设置在按钮而不是文本区域?

package test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EtchedBorder;


public class Test {

    static JPanel southPanel;
    static JButton closeButton;
    static JFrame frame;
    static JTextArea textArea;
    private static final Dimension REASON_AREA_SIZE = new Dimension(250, 50);

    public static void main(String args[]) {
        String title = "";                                                                                                                                                                             
        frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setSize(1000, 800);
        textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setEditable(true);
        JScrollPane lScrollPane = new JScrollPane(textArea);
        lScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        lScrollPane.setPreferredSize(REASON_AREA_SIZE);
        frame.add(lScrollPane, BorderLayout.CENTER);
        frame.add(createSouthPanel(), BorderLayout.SOUTH);

        //Here the close button is defined by default, the focus should be on it
        setDefaultButton();

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setTitle(title);                                                                                                                                                                          
        frame.setVisible(true);
    }

    private static JPanel createSouthPanel() {
        southPanel = new JPanel(
                new FlowLayout(FlowLayout.RIGHT, 10, 10));
        southPanel.setBorder(new EtchedBorder(EtchedBorder.RAISED));
        addCloseButton(southPanel);
        return southPanel;
    }

    /**
     * Set the close button as default
     */
    protected static void setDefaultButton() {
        frame.getRootPane().setDefaultButton(closeButton);
    }

    private static void addCloseButton(JPanel pButtonsPanel) {
        closeButton = new JButton("Close");
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent pEvent) {
                frame.dispose();
            }
        });
        pButtonsPanel.add(closeButton);
    }
}

【问题讨论】:

    标签: java swing focus awt jtextarea


    【解决方案1】:

    在您的 setDefaultButton() 方法中,添加:

    closeButton.requestFocus()
    

    焦点与默认值不同。

    【讨论】:

    • 问题是一样的,光标还在JTextArea中。我无法通过单击 Enter 关闭窗口
    • 现在不在 IDE 前面,但尝试 'frame.getRootPane().closeButton.requestFocus()` - 我怀疑它没有正确找到关闭按钮。
    • 最后,如果我把它放在主函数的末尾,closeButton.requestFocus() 就可以工作了。谢谢
    猜你喜欢
    • 2013-07-04
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    相关资源
    最近更新 更多