Java Swing getRootPane()返回null

mFe*_*ein 2 java swing intellij-idea

我试图使用简单的代码将按钮设置为默认按钮:

mainPanel.getRootPane().setDefaultButton(sendButton);
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Rad*_*def 5

public static void main(String[] args) {
    ...
    frame.setContentPane(new TestSwing().mainPanel);
    ...
}

public TestSwing() {                
    mainPanel.getRootPane().setDefaultButton(sendButton);
}
Run Code Online (Sandbox Code Playgroud)

在将面板添加到框架之前,您将获得根窗格.这里的操作顺序是:

  1. new TestSwing() 叫做.
  2. 实例初始化程序运行并mainPanel创建.
  3. 尝试从mainPanel构造函数中获取根窗格.
  4. mainPanel施工完成后添加到框架中.

因此,您需要重构此操作,以便在将面板添加到框架后获取根窗格.换句话说,不要在构造函数中执行此操作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() {                
}
Run Code Online (Sandbox Code Playgroud)

此外,您应该阅读Swing教程,尤其是"初始线程".您需要main在调用中包装GUI创建(内容),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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我通常这样做,因为我觉得它很好,很整洁:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

它可以通过多种方式完成.关键是,最好让您的创建代码以统一的方式进行逻辑分组.如果你正在做一些main,其中一些在实例初始化器中,其中一些在构造函数中等,则很难遵循.同样在这种特殊情况下,根窗格实际上是JFrame的一个属性,因此将它与JFrame配置放在一起似乎合乎逻辑.

如果您正在使用GUI构建器,我认为它更难管理,因为您无法在类中对组件进行分组.