JTextArea 和 JButton。将焦点设置在按钮上

JTextArea and JButton. Set the focus on the button

下面的程序显示了一个 JTextArea 和一个 "Close" JButton。我将该按钮定义为默认按钮,因为我希望它在我打开 window 时立即获得焦点。但是,当我 运行 程序时,按钮突出显示但焦点位于文本字段上。

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

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

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

closeButton.requestFocus()

焦点与默认不同。