这是我的项目:https://github.com/Kolyall/GUIExample
MainClass
import javax.swing.*;
public class MainClass {
public static void main(String[] args) {
SwingUtilities.invokeLater(MainFrame::new);
}
}
MainFrame
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame {
public MainFrame() {
super("MainFrame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setMinimumSize(new Dimension(850, 650));
setSize(new Dimension(850, 650));
JPanel rootPanel = new JPanel();
rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.X_AXIS));
getContentPane().add(rootPanel);
setLocationRelativeTo(null);
setVisible(true);
ButtonsFrame buttonsFrame = new ButtonsFrame()
rootPanel.add(buttonsFrame.buttonsPanel);//java.lang.NullPointerException in this line
}
}
ButtonsFrame
import javax.swing.*;
public class ButtonsFrame {
public JButton button1Button;
public JButton button2Button;
public JPanel buttonsPanel;
}
ButtonsFrame.form
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.github.kolyall.test.ButtonsFrame">
<grid id="27dc6" binding="buttonsPanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="70295" class="javax.swing.JButton" binding="button1Button" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Button1"/>
</properties>
</component>
<vspacer id="3d1dd">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="76c52" class="javax.swing.JButton" binding="button2Button" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Button2"/>
</properties>
</component>
</children>
</grid>
</form>
但是运行MainClass.main()
之后发生错误:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1095)
at java.awt.Container.add(Container.java:419)
at com.github.kolyall.test.MainFrame.<init>(MainFrame.java:29)
似乎IntelliJ IDEA不会生成二进制类文件,但是该选项已启用,我还尝试了“ Java源代码”
UPDATE: Idea的已知问题 Why swing GUI form builder doesn't generate binary class files/java source code?
这与二进制代码生成无关。您的代码看起来丑陋和错误。
ButtonsFrame buttonsFrame = new ButtonsFrame()
rootPanel.add(buttonsFrame.buttonsPanel);
您在这里得到了NPE,因为尚未在ButtonsFrame
类中初始化buttonsPanel。
例如:
public class ButtonsFrame {
public JButton button1Button = new JButton("OK");
public JButton button2Button = new JButton("Cancel");
public JPanel buttonsPanel = new JPanel();
}
但是公共成员变量在OOP中不受欢迎。更好的方法是提供一种创建可立即使用的按钮面板的方法。
public class ButtonsFrame {
private JButton button1Button = new JButton("OK");
private JButton button2Button = new JButton("Cancel");
private JPanel buttonsPanel = new JPanel();
public JPanel createButtonsPanel() {
buttonsPanel.removeAll();
buttonsPanel.add(button1Button);
buttonsPanel.add(button2Button);
return buttonsPanel;
}
}
并稍后使用:
rootPanel.add(buttonsFrame.createButtonsPanel());