Intellij,"contentPane不能设置为null".使用秋千设计师 [英] Intellij, "contentPane cannot be set to null" using swing designer

查看:52
本文介绍了Intellij,"contentPane不能设置为null".使用秋千设计师的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试intellij,并且正在尝试一些摇摆开发.我遇到了一个我从未在日食方面遇到过的问题,我想知道我的设置是否有误.

I'm trying out intellij, and am trying to do some swing development. I am running into an issue I have never experienced on eclipse, and I am wondering if I have something set up wrong.

这是我的驱动程序运行的GUI类:

Here is my GUI class that is run by my driver:

package view;

import javax.swing.*;
import java.awt.*;

public class View {

private JPanel panel;

public void run() {
    JFrame frame = new JFrame("Vending Machine");
    frame.setContentPane(new View().panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

{
// 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$$$() {
    final JPanel panel1 = new JPanel();
    panel1.setLayout(new GridBagLayout());
}

}

我的run()方法非常简单.但是,在编译时,这是我收到的错误:

as far as I can tell, my run() method is as straightforward as it gets. However, upon compiling, this is the error I receive:

Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
at javax.swing.JRootPane.setContentPane(JRootPane.java:621)
at javax.swing.JFrame.setContentPane(JFrame.java:698)
at view.View.run(View.java:13)
at model.VendingMachine.<init>(VendingMachine.java:14)
at controller.Driver.main(Driver.java:14)

无论出于何种原因,intellij自动创建的代码都无法正确初始化JPanel,这就是为什么它为空的原因.

For whatever reason, the intellij autocreated code that does not properly initialize the JPanel, which is why it's null.

我尝试自己在运行中实例化它(panel = new JPanel();),但这没有帮助.

I've tried instantiating it myself inside run (panel = new JPanel();) but that has not helped.

这很明显吗?开始使用Eclipse中的Swing时,我从来没有遇到过这个问题.

Is this something obvious? I've never run into this issue when getting started with swing in eclipse.

推荐答案

尝试在Intellij IDEA中启用"UI Designer"插件,这对我来说很有帮助.文件->设置->插件-> UI Designer->重新启动IDE

Try enabling "UI Designer" plugin in Intellij IDEA, it helped in my case. File -> Settings -> Plugins -> UI Designer -> Restart IDE

这篇关于Intellij,"contentPane不能设置为null".使用秋千设计师的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆