2017-03-16 4 views
-1

Ich benutze NetBeans 8.2 auf OSX und wenn ich meinen Code ausführen, bekomme ich ein Fenster, das den richtigen Titel hat, aber keine Komponenten erscheinen. Ich habe sichergestellt, dass alle meine Frames auf sichtbar gesetzt sind, aber das Problem bleibt bestehen.JFrame fehlt Inhalt GridBagLayout mit Panels

public Project(){ 
    setTitle("Panel"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(400, 200); 
    setLocationRelativeTo(null); 
    setLayout(new GridBagLayout()); 
    setVisible(true); 

    GridBagConstraints layout = new GridBagConstraints(); 
    layout.gridy = 3; 

    JPanel topPanel = new JPanel(); 
    topPanel.setVisible(true); 
    topPanel.setLayout(new GridLayout(2,1)); 
    topPanel.add(label1); 
    topPanel.add(textField1); 

    JPanel midPanel = new JPanel(); 
    midPanel.setVisible(true); 
    midPanel.setLayout(new GridLayout(1,1)); 
    midPanel.add(button1); 

    JPanel bottomPanel = new JPanel(); 
    bottomPanel.setVisible(true); 
    bottomPanel.setLayout(new GridLayout(2,1)); 
    bottomPanel.add(label2); 
    bottomPanel.add(textField2); 

    add(topPanel); 
    add(midPanel); 
    add(bottomPanel); 
    } 

    public static void main(String[] args) { 
    Project1 frame = new Project1(); 
    } 

Antwort

1

setVisible(true); nehmen und das letzte, was machen Sie in Ihrem Konstruktor aufrufen können

public Project(){ 
    setTitle("Panel"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(400, 200); 
    setLocationRelativeTo(null); 
    setLayout(new GridBagLayout()); 
    //setVisible(true); 
    //... 
    setVisible(true); 
} 

Sie sollten auch sicherstellen, dass die Benutzeroberfläche wurde im Rahmen der Veranstaltung Dispatching Thema

aufgebaut ist
public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      Project1 frame = new Project1(); 
     } 
    }); 
} 

Das wird helfen, einige Kuriositäten zu verhindern, die auftreten können