0

我生成了一个带有 JFormDesigner 的 JDialog,但我不知道如何显示它。这是代码:

    public class FinishPopup extends JDialog {
        public FinishPopup(Frame owner) {
            super(owner);
            initComponents();
        }

    public FinishPopup(Dialog owner) {
        super(owner);
        initComponents();
    }

    public void initComponents() {
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        label1 = new JLabel();
        label2 = new JLabel();
        buttonBar = new JPanel();
        okButton = new JButton();
        label3 = new JLabel();

        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        {

            {

                label1.setText("Money: ");
                label1.setFont(new Font("Tahoma", Font.PLAIN, 12));


                label2.setText("Time");
                label2.setFont(new Font("Tahoma", Font.PLAIN, 12));

                GroupLayout contentPanelLayout = new GroupLayout(contentPanel);
                contentPanel.setLayout(contentPanelLayout);
                contentPanelLayout.setHorizontalGroup(
                    contentPanelLayout.createParallelGroup()
                        .addGroup(contentPanelLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(label2)
                            .addGap(90, 90, 90)
                            .addComponent(label1)
                            .addContainerGap(105, Short.MAX_VALUE))
                );
                contentPanelLayout.setVerticalGroup(
                    contentPanelLayout.createParallelGroup()
                        .addGroup(contentPanelLayout.createSequentialGroup()
                            .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(contentPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(label2)
                                .addComponent(label1))
                            .addContainerGap())
                );
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0};

                okButton.setText("OK");
                buttonBar.add(okButton, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);

            label3.setText("Finished");
            label3.setFont(new Font("Tahoma", Font.BOLD, 14));
            dialogPane.add(label3, BorderLayout.NORTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
    }

    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel label1;
    private JLabel label2;
    private JPanel buttonBar;
    private JButton okButton;
    private JLabel label3;
}

如何创建和展示它?谢谢。

4

1 回答 1

0

您需要首先创建该类的一个实例,然后调用setVisible使其可见

// owner is reference to the parent frame or dialog.  May be null, but you'll 
// need to cast it
FinishPopup popup = new FinishPopup(owner);
popup.setVisible(true);

您可能还会发现setLocationRelativeTo有帮助

于 2013-06-24T07:18:50.307 回答