Java窗口是空的

时间:2012-06-24 11:40:08

标签: java swing

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

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();
}

1 个答案:

答案 0 :(得分:6)

一旦将所有组件添加到表单之前,您应该致电setVisible(true);。 另外,请在最后删除多余的repaint()电话。

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