以下程序显示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);
}
}
。我将按钮定义为默认按钮,因为我希望在打开窗口后立即获得焦点。但是,当我运行该程序时,该按钮突出显示,但焦点位于文本字段上。
如何将焦点设置在按钮上而不是文本区域上?
{{1}}
答案 0 :(得分:1)
在您的setDefaultButton()
方法内,添加:
closeButton.requestFocus()
焦点与默认焦点不同。