Java Swing getRootPane()返回null

时间:2014-11-17 16:12:35

标签: java swing intellij-idea

我正在尝试使用简单代码将按钮设置为默认按钮:

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

1 个答案:

答案 0 :(得分:5)

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

public TestSwing() {                
    mainPanel.getRootPane().setDefaultButton(sendButton);
}

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

  1. new TestSwing()被召唤。
  2. 运行实例初始值设定项并创建mainPanel
  3. 尝试从构造函数中的mainPanel获取根窗格。
  4. 构建完成后,将mainPanel添加到框架中。
  5. 因此,您需要重构此内容,以便在将面板添加到框架后获取根窗格。换句话说,不要在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'。您需要在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);
        }
    }
    

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

    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中做了一些,其中一些在实例初始化器中,其中一些在构造函数中等,则很难遵循。同样在这种特殊情况下,根窗格实际上是JFrame的一个属性,所以将它放在JFrame配置中似乎合乎逻辑。

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