从独立的JFormDesigner运行GUI

时间:2018-10-15 12:23:04

标签: java swing

我已经独立地在JFormDesigner中构建了程序,将其保存,并且在我运行它时没有任何显示。我需要编辑或键入什么才能显示它? 谢谢。

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个按钮。

1 个答案:

答案 0 :(得分:1)

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

private void initComponents() {
    // your mentioned code

    setVisible(true);
}