Question

question plutôt basique ici gars.

En fait, j'ai le code comme ceci:

public SuperPanel() {
    setLayout(new BorderLayout());
    add(panel1(), BorderLayout.NORTH);
    add(panel2(), BorderLayout.CENTER);
    add(panel3(), BorderLayout.SOUTH);
}

Et que tout fonctionne bien et bon. Le problème est que j'ai une autre partie que je voudrais ajouter au centre. Juste en utilisant add(newPanel(), BorderLayout.CENTER) ne fonctionne pas, évidemment. Mais vous pouvez ajouter à JPanels JPanels, correct?

Je fait le changement suivant:

public SuperPanel() {
    setLayout(new BorderLayout());
    add(panel1(), BorderLayout.NORTH);
    add(supersweetpanel(), BorderLayout.CENTER);
    add(panel3(), BorderLayout.SOUTH);
}

étant supersweetpanel():

public JPanel supersweetpanel() {
    JPanel sswp = new JPanel();
    setLayout(new BorderLayout());
    add(panel2(), BorderLayout.NORTH);
    return sswp;
}

Maintenant, il remplace panel1! Si je mets à quoi que ce soit d'autre (CENTER, SOUTH, qu'avez-vous), les deux premiers panneaux disparaissent complètement. L'aide est très appréciée.

Était-ce utile?

La solution

SuperPanel est probablement une sous-classe de JPanel, non? Vous ajoutez accidentellement panel2 à this (le SuperPanel), non sswp. Essayez:

public JPanel supersweetpanel() {
        JPanel sswp = new JPanel();
        sswp.setLayout(new BorderLayout());
        sswp.add(panel2(), BorderLayout.NORTH);
        return sswp;
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top