2012-06-24 4 views
2

J'ai un problème étrange. Je crée cette fenêtre, mais elle est vide jusqu'à ce que je la minimise et la maximise à nouveau. Est-ce un problème courant? Voici mon code (Sry à cause du long code .... J'ai seulement supprimé les écouteurs d'action, j'avais peur de pouvoir effacer l'erreur).La fenêtre Java est vide

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

Je pense que vous ne devriez pas le mettre à visible jusqu'à ce que vous ajoutiez tous vos composants. Et il n'y a pas besoin d'appeler «repeindre» – GETah

+0

ohh, j'ai oublié de le supprimer;) ... a été mon premier essai pour résoudre ce problème – Leander

+0

Voir ma réponse ci-dessous – GETah

Répondre

6

Vous devez appeler setVisible(true); une fois que tous vos composants sont ajoutés à la forme pas avant. En outre, supprimez l'appel repaint() superflu à la fin.

EDIT Ajouté le commentaire de Andrew. Notez que pack() ou validate() doit être appelé avant setVisible(true) pour une interface graphique de manière fiable. Merci @Andrew Thompson

+0

merci. Son fonctionnement: D – Leander

+0

Excellent! Heureux qu'il a aidé – GETah

+0

@AndrewThompson Mis à jour ma réponse avec vos bons points! – GETah