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

时间:2016-10-22 00:18:55

标签: java swing miglayout

我创建了一个名为CreateNewGraph的类,它扩展了JDialog,这个类是从一个单独的类中调用的。第一次调用它时,它的行为与预期的一样,但在此之后的任何时候,布局(我正在使用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个,但我为了简洁而删除了这些按钮。通常情况下,我不会发布这么多代码,但我不确定错误发生在哪里(没有抛出异常。)

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

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

第一次打开时会发生这种情况: First time

这第一种情况正是它应该是什么样子。但第二次,这发生了: Second time small 这是第二种情况,扩展到填满我的屏幕: Second time full

1 个答案:

答案 0 :(得分:3)

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