2

我有一个奇怪的问题。我创建了这个窗口,但它是空白的,直到我最小化它并再次最大化它。这是个常见的问题吗?这是我的代码(对不起,因为代码很长......我只删除了动作监听器,我担心我可以删除错误)。

public class Gui_preferences extends JFrame{
private static final long serialVersionUID = -2375617814937747645L;
private JTextField savingPathField;
private JTextField schoolField;
private JTextField classStageField;
private JFormattedTextField classdurationField;
private FunctionsSchedule fkSchedule;
JFormattedTextField schoolbeginningformattedTextField;
boolean firstopening;
JLabel notClosingLabel;
private JTextField formsFolderNameField;
private JTextField subjectsFolderNameField;

public Gui_preferences(boolean first, FunctionsSchedule fktSchedule) {
    this.fkSchedule = fktSchedule;
    setPreferredSize(new Dimension(650, 420));
    setSize(650, 420);
    setResizable(false);
    setAlwaysOnTop(true);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setVisible(true);

    JScrollPane scrollPane = new JScrollPane();
    getContentPane().add(scrollPane, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(500, 420));
    panel.setMinimumSize(new Dimension(500, 420));
    scrollPane.setViewportView(panel);
    panel.setLayout(new MigLayout("", "[][][grow][][][]", "[11.00][][12.00][][16.00][][17.00][][18.00][][16.00][24.00][15.00][24.00][15.00][][19.00][]"));

    JLabel savingPathLabel = new JLabel("New label");
    panel.add(savingPathLabel, "cell 0 1");

    savingPathField = new JTextField();
    savingPathField.setEditable(false);
    panel.add(savingPathField, "cell 2 1,growx");
    savingPathField.setColumns(10);

    JButton newSavingPath = new JButton("New button");
    panel.add(newSavingPath, "cell 4 1");

    JLabel school = new JLabel("New label");
    panel.add(school, "cell 0 3");

    schoolField = new JTextField();
    schoolField.addFocusListener(new FocusAdapter() {
    panel.add(schoolField, "cell 2 3,growx");
    schoolField.setColumns(10);

    JLabel classStage = new JLabel("New label");
    panel.add(classStage, "cell 0 5");
    classStageField = new JTextField();
    panel.add(classStageField, "cell 2 5,growx");
    classStageField.setColumns(10);

    JLabel classduration = new JLabel("New label");
    panel.add(classduration, "cell 0 9");

    classdurationField = new JFormattedTextField(createFormatter("###"));
    panel.add(classdurationField, "cell 2 9,growx");
    classdurationField.setColumns(10);

    JLabel schoolbeginning = new JLabel("New label");
    panel.add(schoolbeginning, "cell 0 7");

    schoolbeginningformattedTextField = new JFormattedTextField(createFormatter("##:##"));
    panel.add(schoolbeginningformattedTextField, "cell 2 7,growx");

    JLabel TrayIcon = new JLabel("New label");
    panel.add(TrayIcon, "cell 0 11");

    JCheckBox TrayIconCheckBox = new JCheckBox("New check box");
    panel.add(TrayIconCheckBox, "cell 2 11");

    JLabel otherFolderName = new JLabel("New label");
    panel.add(otherFolderName, "cell 0 13");

    formsFolderNameField = new JTextField();
    panel.add(formsFolderNameField, "cell 2 13,growx");
    formsFolderNameField.setColumns(10);

    JLabel subjectsFolderName = new JLabel("New label");
    panel.add(subjectsFolderName, "cell 0 15");

    subjectsFolderNameField = new JTextField();
    panel.add(subjectsFolderNameField, "cell 2 15,growx");
    subjectsFolderNameField.setColumns(10);

    notClosingLabel = new JLabel("New label");
    notClosingLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
    panel.add(notClosingLabel, "cell 2 17");

    //own code
    firstopening = first;
    //Labels & Co get the names
    savingPathLabel.setText(NonserData.exprs.getString("saving_Path")+": ");
    school.setText(NonserData.exprs.getString("school")+": ");
    classStage.setText(NonserData.exprs.getString("classStage")+": ");
    classduration.setText(NonserData.exprs.getString("classduration")+": ");
    TrayIcon.setText(NonserData.exprs.getString("TrayIcon")+": ");
    schoolbeginning.setText(NonserData.exprs.getString("schoolbeginning")+": ");
    TrayIconCheckBox.setText(NonserData.exprs.getString("enabled"));
    newSavingPath.setText(NonserData.exprs.getString("choose"));
    otherFolderName.setText(NonserData.GUI.getString("forms_folder_name")+": ");
    subjectsFolderName.setText(NonserData.GUI.getString("subject_Folder_name")+": ");

    //Checkbox activation/deactivation
    TrayIconCheckBox.setSelected(SerData.trayIconenabled);


    notClosingLabel.setEnabled(false);
    if(first)
    {
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        notClosingLabel.setEnabled(true);
        notClosingLabel.setText(NonserData.GUI.getString("not_cosing_before_filled"));
    }
    if(!first)
    {
        notClosingLabel.setVisible(false);
        notClosingLabel.setEnabled(false);
    }
    fillTextBoxes();
    repaint();
}
4

1 回答 1

6

setVisible(true);一旦所有组件都添加到表单中,您就应该调用而不是之前。另外,在最后删除多余的repaint()调用。

编辑添加了安德鲁的评论。请注意,pack()validate()应该在之前调用setVisible(true)以获取可靠布局的 GUI。谢谢@安德鲁汤普森

于 2012-06-24T11:45:24.930 回答