JLabel only shows if initComponents() is deleted(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))
);
这篇关于JLabel 仅显示是否删除了 initComponents()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JLabel 仅显示是否删除了 initComponents()
基础教程推荐
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01