gpt4 book ai didi

java - 为什么 swing GUI 表单生成器不生成二进制类文件/java 源代码?

转载 作者:行者123 更新时间:2023-12-01 18:42:32 24 4
gpt4 key购买 nike

这是我的项目:https://github.com/Kolyall/GUIExample

主类

import javax.swing.*;

public class MainClass {

public static void main(String[] args) {
SwingUtilities.invokeLater(MainFrame::new);
}

}

主机

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
}

}

按钮框架

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源代码” enter image description here

更新: 这是 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 类中初始化 ButtonPanel。

例如:

public class ButtonsFrame {
public JButton button1Button = new JButton("OK");
public JButton button2Button = new JButton("Cancel");
public JPanel buttonsPanel = new JPanel();
}

但是公共(public)成员变量在 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());

关于java - 为什么 swing GUI 表单生成器不生成二进制类文件/java 源代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59896471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com