Affichage de composants (avec NetBeans)
Bonjour,
je fais un petit programme de test, et je rencontre un problème lié (apparamment) à NetBeans.
Voila ma fenêtre principale :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| public class MyFrame extends javax.swing.JFrame {
/** Creates new form MyFrame */
public MyFrame()
{
initComponents();
JLabel l = new JLabel("Salut");
l.setVisible(true);
getContentPane().add(l);
validate();
repaint();
pack();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Frame");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(116, 116, 116)
.add(jLabel1)
.addContainerGap(254, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap(146, Short.MAX_VALUE)
.add(jLabel1)
.add(140, 140, 140))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new MyFrame().setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
} |
Comme vous pouvez le voir dans le code généré automatiquement, j'ai placé un JLabel contenant le texte "Frame" dans ma fenêtre. Donc ce JLabel est instancié et positionné grâce à la méthode initComponents().
Ensuite je veux rajouter, par programmation, un autre JLabel, contenant le texte "Salut", mais problème : il ne s'affiche pas !
Au lancement, seul le label "Frame" est affiché, je ne comprends pas pourquoi.
En revanche si j'enlève l'appel à la méthode initComponents(), j'ai bien mon label "Salut" mais plus le "Frame" (normal).
Savez-vous comment je peux faire pour pouvoir à la fois positionner des composants avec l'éditeur de netbeans et en ajouter par programmation ?
Merci d'avance :)
ps : j'ai essayé de faire un getContentPane().removeAll() avant mon getContentPane().add(l), ça enlève bien le "Frame", mais le "Salut" n'apparaît toujours pas :o
ps2 : en modifiant le layout que netbeans génère et en mettant un FlowLayout j'ai bien les 2 labels, mais du coup je perds la présentation :(
Si vous savez comment faire cohabiter les 2 je vous en serai très reconnaissant^^