ejemplo - jframe en java
JLabel solo muestra si se elimina initComponents() (1)
MainFrame.java -JFrame
public MainFrame() {
initComponents();
Letters pl = new Letters(this);
this.setContentPane(pl);
this.setTitle("Preset Lessons");
this.pack();
}
Letters.java -JPanel
public Letters(JFrame frame) {
initComponents();
JLabel label = new JLabel();
label.setText("Sample");
this.add(label);
}
si se elimina initComponents () en Letters.java, esa es la única vez que se mostrará JLabel. ¿Cómo puedo poner el nuevo JLabel en mi JPanel existente?
Contenido de los initComponents de Letters.java (); si eliminé los iniComponents en el constructor creará JLabel.
private void initComponents() {
jLabelLetters = new javax.swing.JLabel();
jLabelNumbers = new javax.swing.JLabel();
jLabelFlashcards = new javax.swing.JLabel();
jLabelStories = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
setMaximumSize(new java.awt.Dimension(333333, 333333));
setMinimumSize(new java.awt.Dimension(800, 600));
setPreferredSize(new java.awt.Dimension(800, 600));
jLabelLetters.setFont(new java.awt.Font("Tahoma", 1, 18));
jLabelLetters.setText("Letters");
jLabelNumbers.setFont(new java.awt.Font("Tahoma", 0, 14));
jLabelNumbers.setText("Numbers");
jLabelNumbers.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabelNumbersMouseClicked(evt);
}
});
jLabelFlashcards.setFont(new java.awt.Font("Tahoma", 0, 14));
jLabelFlashcards.setText("Flashcards");
jLabelFlashcards.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabelFlashcardsMouseClicked(evt);
}
});
jLabelStories.setFont(new java.awt.Font("Tahoma", 0, 14));
jLabelStories.setText("Stories");
jLabelStories.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabelStoriesMouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jLabelLetters)
.addGap(75, 75, 75)
.addComponent(jLabelNumbers)
.addGap(76, 76, 76)
.addComponent(jLabelFlashcards)
.addGap(75, 75, 75)
.addComponent(jLabelStories)
.addGap(89, 89, 89)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(32, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabelFlashcards)
.addComponent(jLabelStories))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jComboBox1, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabelLetters)
.addComponent(jLabelNumbers)))
.addGap(565, 565, 565))
);
}// </editor-fold>
El problema es que al usar GroupLayout, todos los componentes en el contenedor deben agregarse a los grupos paralelos horizontales y verticales del diseño:
En Letters.java
, ha agregado JLabel
al contenedor pero no lo ha unido a los 2 grupos del diseño, por lo que no se mostrará nada como resultado.
Para solucionarlo, puede mover la creación de la initComponents
método initComponents
y agregarla a los grupos:
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jLabelLetters)
.addGap(75, 75, 75)
.addComponent(jLabelNumbers)
.addGap(76, 76, 76)
.addComponent(jLabelFlashcards)
.addGap(75, 75, 75)
.addComponent(jLabelStories)
.addGap(89, 89, 89)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(32, Short.MAX_VALUE))
.addComponent(label)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabelFlashcards)
.addComponent(jLabelStories))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jComboBox1, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabelLetters)
.addComponent(jLabelNumbers)))
.addComponent(label)
.addGap(565, 565, 565))
);