JLabel 仅显示是否删除了 initComponents()
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);
}
如果 Letters.java 中的 initComponents() 被删除,那只有 JLabel 才会出现.如何将新的 JLabel 放入现有的 JPanel?
if initComponents() in Letters.java is deleted thats the only time the JLabel will show up. How can I put the new JLabel to my existing JPanel?
Letters.java 的 initComponents() 的内容;如果我在构造函数中删除了 iniComponents,它将创建 JLabel.
Contents of Letters.java's initComponents(); if I removed the iniComponents in the constructor it will create the 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>
推荐答案
问题是在使用GroupLayout 容器上的所有组件都必须添加到水平 &布局的垂直平行组:
The problem is that when using GroupLayout all components on the container must be added to both the horizontal & vertical parallel groups of the layout:
在 Letters.java
中,您已将 JLabel
添加到容器中,但尚未将其附加到 2 组布局中,因此不会显示为结果.
In Letters.java
, you have added the JLabel
to the container but have not attached it to the 2 groups of the layout, so nothing will be displayed as a result.
要修复,您可以将标签创建移动到 initComponents
方法并添加到组中:
To fix, you could move the label creation to the initComponents
method and add to the groups:
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))
);
相关文章