JDialog 每次创建时都有不同的行为

JDialog has different behavior each time it is created

我创建了一个名为 CreateNewGraph 的 class,它扩展了 JDialog,这个 class 是从一个单独的 class 调用的。第一次调用时,它的行为符合预期,但此后的任何时候,布局(我使用的是 MigLayout)都搞砸了:组件间距不正确,每个组件都添加了两次。

提前抱歉了这么多代码,我想把所有的都包括进来,因为我不知道问题出在哪里。

这是 CreateNewGraph 的代码:

public class CreateNewGraph extends JDialog {

private static final JPanel contentPanel = new JPanel();
private static JPanel buttonPanel;
private static JTextField txtFldName;
private static Font directionFont = new Font("TimesRoman", Font.PLAIN, 15);
private static JTextPane txtPaneTypeError, txtPaneNameError, txtPaneEnterName;
private static JButton okButton, cancelButton;
private static JRadioButton rdbtnXYGraph, rdbtnTimeGraph;
private static ButtonGroup kind;
private static JLabel lblWhichKind, lblName;

/**
 * Create the dialog.
 */
public CreateNewGraph() {
    setTitle("Add a New Graph");
    setModalityType(ModalityType.APPLICATION_MODAL);
    setModal(true);
    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    getContentPane().setLayout(new BorderLayout());

    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    contentPanel.setLayout(new MigLayout("", "[grow]", "[][][][][][][grow][grow]"));

    lblWhichKind = new JLabel("What kind of graph would you like to add? You may select only one.");
    lblWhichKind.setFont(directionFont);
    contentPanel.add(lblWhichKind, "cell 0 0");

    kind = new ButtonGroup();

    Component strutRadioBtns = Box.createHorizontalStrut(20);
    contentPanel.add(strutRadioBtns, "flowx,cell 0 1");

    rdbtnXYGraph = new JRadioButton("XY Graph");
    contentPanel.add(rdbtnXYGraph, "cell 0 1");
    kind.add(rdbtnXYGraph);

    rdbtnTimeGraph = new JRadioButton("Time Graph");
    contentPanel.add(rdbtnTimeGraph, "cell 0 1");
    kind.add(rdbtnTimeGraph);

    Component verticalStrut = Box.createVerticalStrut(10);
    contentPanel.add(verticalStrut, "cell 0 2");

    txtPaneEnterName = new JTextPane();
    txtPaneEnterName.setText("What would you like to name this graph? It must be a unique name (you may not use one you have already used.)");
    txtPaneEnterName.setFont(directionFont);
    txtPaneEnterName.setEditable(false);
    txtPaneEnterName.setOpaque(false);
    contentPanel.add(txtPaneEnterName, "cell 0 3,grow");

    Component horizontalStrut = Box.createHorizontalStrut(20);
    contentPanel.add(horizontalStrut, "flowx,cell 0 4");

    lblName = new JLabel("Name:");
    contentPanel.add(lblName, "cell 0 4");

    txtFldName = new JTextField();
    contentPanel.add(txtFldName, "cell 0 4");
    txtFldName.setColumns(10);

    Component verticalStrut_1 = Box.createVerticalStrut(10);
    contentPanel.add(verticalStrut_1, "cell 0 5");

    txtPaneTypeError = new JTextPane();
    txtPaneTypeError.setText("Graph Type Error: ");
    txtPaneTypeError.setEditable(false);
    txtPaneTypeError.setOpaque(false);
    contentPanel.add(txtPaneTypeError, "cell 0 6,grow");

    txtPaneNameError = new JTextPane();
    txtPaneNameError.setText("Graph Name Error: ");
    txtPaneNameError.setEditable(false);
    txtPaneNameError.setOpaque(false);
    contentPanel.add(txtPaneNameError, "cell 0 7,grow");

    buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    getContentPane().add(buttonPanel, BorderLayout.SOUTH);

    okButton = new JButton("OK");
    okButton.setActionCommand("OK");
    buttonPanel.add(okButton);

    cancelButton = new JButton("Cancel");
    buttonPanel.add(cancelButton);
    getRootPane().setDefaultButton(cancelButton);
}

我在每个按钮上也有 ActionListener,但为了简洁起见,我删除了它们。通常,我不会 post 这么多代码,但我不确定错误发生在哪里(没有抛出异常。)

而且,我在另一个 class 中使用的代码来创建 CreateNewGraph 的实例:

EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        CreateNewGraph add = new CreateNewGraph();
                        add.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

这是第一次打开时发生的情况:

第一个案例正是它应该的样子。但是第二次,会发生这种情况: 这是第二种情况,展开以填满我的屏幕:

您正在用重新添加到破坏 GUI 的静态 JPanel 的静态组件来自杀。解决方案:使所有 JDialog class 字段成为实例字段。这将允许您在创建对象时重新创建它们,这样您就不会破坏您的布局。一个额外的好处是您还将遵循良好的 OOP 实践。