gpt4 book ai didi

java - 从独立的 JFormDesigner 运行 GUI

转载 作者:行者123 更新时间:2023-12-02 10:42:40 26 4
gpt4 key购买 nike

我已经在 J​​FormDesigner 中独立构建了程序,保存了它,当我运行它时没有任何显示。我需要编辑或输入什么才能显示?谢谢。

public class Something extends JFrame {
public Something() {
initComponents();
}

private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner Evaluation license - Vanja Banic
label1 = new JLabel();
button1 = new JButton();
button2 = new JButton();

//======== this ========
Container contentPane = getContentPane();
contentPane.setLayout(null);

//---- label1 ----
label1.setText("Something");
contentPane.add(label1);
label1.setBounds(0, 0, 585, 39);

//---- button1 ----
button1.setText("text");
contentPane.add(button1);
button1.setBounds(new Rectangle(new Point(95, 160), button1.getPreferredSize()));

//---- button2 ----
button2.setText("text");
contentPane.add(button2);
button2.setBounds(new Rectangle(new Point(285, 160), button2.getPreferredSize()));

{ // compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < contentPane.getComponentCount(); i++) {
Rectangle bounds = contentPane.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = contentPane.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
contentPane.setMinimumSize(preferredSize);
contentPane.setPreferredSize(preferredSize);
}
pack();
setLocationRelativeTo(getOwner());

}


// Generated using JFormDesigner Evaluation license - Vanja Banic
private JLabel label1;
private JButton button1;
private JButton button2;
// JFormDesigner - End of variables declaration //GEN-END:variables
}

我知道它现在缺少 main() 并且我不知道要在其中写什么来显示程序。此示例将显示 1 个标签和 2 个按钮。

最佳答案

只需将 setVisible(true); 添加到 initComponents() 方法的末尾即可:

private void initComponents() {
// your mentioned code

setVisible(true);
}

关于java - 从独立的 JFormDesigner 运行 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52816712/

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