我正在尝试使用简单的代码将按钮设置为默认按钮:
mainPanel.getRootPane().setDefaultButton(sendButton);
但是getRootPane()
返回null
。我正在使用 IntelliJ Idea Form 设计器,我的代码基本上就是类构造函数中的那一行,就在 main 之后:
public static void main(String[] args) {
JFrame frame = new JFrame("TestSwing");
frame.setContentPane(new TestSwing().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(700, 700));
frame.pack();
frame.setVisible(true);
}
public TestSwing() {
mainPanel.getRootPane().setDefaultButton(sendButton);
}
IntelliJ 添加了一些隐藏代码,但我看不出这些代码如何改变事物。
我的目标只是在表单中设置一个默认按钮,因此 Enter
将始终激活此按钮。
PS:我是 Swing 新手,但不是 Java。
编辑:
按照要求的所有隐藏代码:
package com.testswing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class TestSwing {
private JPanel mainPanel;
private JTextArea messageTextArea;
private JTextPane chatTextPane;
private JCheckBox autoCheckbox;
private JButton newChatButton;
private JButton sendButton;
private JTextField textField1;
private JTextField textField2;
public static void main(String[] args) {
JFrame frame = new JFrame("TestSwing");
frame.setContentPane(new TestSwing().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(700, 700));
frame.pack();
frame.setVisible(true);
}
public TestSwing() {
mainPanel.getRootPane().setDefaultButton(sendButton);
}
{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
$$$setupUI$$$();
}
/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* @noinspection ALL
*/
private void $$$setupUI$$$() {
mainPanel = new JPanel();
mainPanel.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(5, 2, new Insets(15, 15, 15, 15), -1, -1));
mainPanel.setMinimumSize(new Dimension(500, 500));
messageTextArea = new JTextArea();
messageTextArea.setLineWrap(true);
mainPanel.add(messageTextArea, new com.intellij.uiDesigner.core.GridConstraints(1, 0, 3, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, new Dimension(100, 100), null, null, 0, false));
chatTextPane = new JTextPane();
chatTextPane.setEditable(false);
mainPanel.add(chatTextPane, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, new Dimension(500, 500), new Dimension(150, 50), null, 0, false));
autoCheckbox = new JCheckBox();
autoCheckbox.setLabel("Auto");
autoCheckbox.setText("Auto");
mainPanel.add(autoCheckbox, new com.intellij.uiDesigner.core.GridConstraints(1, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
newChatButton = new JButton();
newChatButton.setText("New");
mainPanel.add(newChatButton, new com.intellij.uiDesigner.core.GridConstraints(2, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
sendButton = new JButton();
sendButton.setText("Send");
mainPanel.add(sendButton, new com.intellij.uiDesigner.core.GridConstraints(3, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final JPanel panel1 = new JPanel();
panel1.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 4, new Insets(0, 0, 0, 0), -1, -1));
mainPanel.add(panel1, new com.intellij.uiDesigner.core.GridConstraints(4, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
final JLabel label1 = new JLabel();
label1.setText("Text To Auto Send:");
panel1.add(label1, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
textField1 = new JTextField();
panel1.add(textField1, new com.intellij.uiDesigner.core.GridConstraints(0, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
final JLabel label2 = new JLabel();
label2.setText("Text To Auto Copare:");
panel1.add(label2, new com.intellij.uiDesigner.core.GridConstraints(0, 2, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
textField2 = new JTextField();
panel1.add(textField2, new com.intellij.uiDesigner.core.GridConstraints(0, 3, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
}
/**
* @noinspection ALL
*/
public JComponent $$$getRootComponent$$$() {
return mainPanel;
}
}
最佳答案
public static void main(String[] args) {
...
frame.setContentPane(new TestSwing().mainPanel);
...
}
public TestSwing() {
mainPanel.getRootPane().setDefaultButton(sendButton);
}
在将面板添加到框架之前,您将获取根 Pane 。这里的操作顺序是:
new TestSwing()
被调用。- 运行实例初始化程序并创建
mainPanel
。 - 尝试从构造函数内的
mainPanel
获取根 Pane 。 - 构建完成后将
mainPanel
添加到框架中。
因此,您需要重构它,以便在将面板添加到框架后获得根 Pane 。换句话说,不要在 TestSwing
的构造函数中执行此操作。无论如何,这样做确实没有意义。它使操作顺序更加难以理解。
举个例子:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
...
TestSwing testSwing = new TestSwing();
frame.setContentPane(testSwing.mainPanel);
frame.getRootPane().setDefaultButton(testSwing.sendButton);
...
}
});
}
public TestSwing() {
}
此外,您应该阅读 Swing 教程,尤其是 'Initial Threads' 。您需要将 GUI 创建(main
中的内容)包装在对 invokeLater
的调用中,以便它在 Swing 事件线程上执行。
就风格而言,官方教程描绘了这样创建的 GUI:
class Example {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame frame = new JFrame();
// setup
frame.setVisible(true);
}
}
我通常这样做,因为我觉得这样既漂亮又整洁:
class Example implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Example());
}
@Override
public void run() {
JFrame frame = new JFrame();
// setup
frame.setVisible(true);
}
}
可以通过多种方式来完成。关键是最好以统一的方式对您的创建代码进行逻辑分组。如果您在 main 中执行某些操作,在实例初始值设定项中执行某些操作,在构造函数中执行某些操作,等等,则很难遵循。同样在这种特殊情况下,根 Pane 实际上是 JFrame 的一个属性,因此对我来说将其与 JFrame 配置放在一起似乎是合乎逻辑的。
如果您使用 GUI 构建器,我认为管理起来会更困难,因为您无法将组件分组到类中。
关于Java Swing getRootPane() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26976910/