首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java ()返回空

Java ()返回空
EN

Stack Overflow用户
提问于 2014-11-17 16:12:35
回答 1查看 2.6K关注 0票数 2

我试图使用简单的代码将按钮设置为默认按钮,如下所示:

代码语言:javascript
复制
mainPanel.getRootPane().setDefaultButton(sendButton);

但是getRootPane()返回null。我使用的是IntelliJ Idea表单设计器,我的代码基本上就是类构造函数中的一行,就在main之后:

代码语言:javascript
复制
 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。

编辑:

所有被请求的隐藏代码:

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-17 17:12:36

代码语言:javascript
复制
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添加到框架中。

因此,您需要重构这一点,以便在将面板添加到框架后获得根窗格。换句话说,不要在TestSwing的构造函数中这样做。不管怎么说,这样做是没有意义的。这使得操作的顺序更难理解。

例如:

代码语言:javascript
复制
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教程,特别是https://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html。您需要将您的GUI创建(main中的内容)封装在对invokeLater的调用中,以便在Swing事件线程上执行。

就风格而言,官方教程描述的GUI是这样创建的:

代码语言:javascript
复制
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);
    }
}

我通常是这样做的,因为我觉得它很漂亮,很整齐:

代码语言:javascript
复制
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构建器,我认为更难管理,因为您不能将组件分组到类中。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26976910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档