JLabel 水平定位未按预期工作
这是一个 JFrame,我打算用一系列具有以下属性的 JLabels 来显示它:
- 堆叠垂直
- 居中水平
- 绿色边框
- 白色背景
- 蓝色文本
但我得到了这个:
蓝色文本、垂直堆叠、绿色边框可以正常工作,但白色背景和水平居中则不行。我还以为标签会跨越 JPanel 的整个宽度。
我做错了什么?
编辑:错过了关于背景颜色的这个问题。所以我剩下的问题是关于 BoxLayout 和组件在另一个轴上的定位。
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class BoxLayoutLabelsTest extends JFrame
{
public BoxLayoutLabelsTest(String title)
{
super(title);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
addLabel(panel, "Hydrogen");
addLabel(panel, "Helium");
addLabel(panel, "Lithium");
addLabel(panel, "Beryllium");
addLabel(panel, "Boron");
setContentPane(panel);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
static private void addLabel(JPanel panel, String text) {
JLabel label = new JLabel(text);
label.setBorder(BorderFactory.createLineBorder(Color.GREEN));
label.setBackground(Color.WHITE);
label.setForeground(Color.BLUE);
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label);
}
public static void main(String[] args) {
new BoxLayoutLabelsTest("BoxLayoutLabelsTest").setVisible(true);
}
}
Here's a JFrame which I intended to show with a series of JLabels with the following properties:
- stacked vertically
- centered horizontally
- green border
- white background
- blue text
But I get this instead:
The blue text, stacked vertically, green border work OK but the white background and centered horizontally do not. I also would have thought the labels would span the entire width of the JPanel.
What am I doing wrong?
edit: Missed this question about background color. So my remaining question is about BoxLayout and the positioning of components in the other axis.
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class BoxLayoutLabelsTest extends JFrame
{
public BoxLayoutLabelsTest(String title)
{
super(title);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
addLabel(panel, "Hydrogen");
addLabel(panel, "Helium");
addLabel(panel, "Lithium");
addLabel(panel, "Beryllium");
addLabel(panel, "Boron");
setContentPane(panel);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
static private void addLabel(JPanel panel, String text) {
JLabel label = new JLabel(text);
label.setBorder(BorderFactory.createLineBorder(Color.GREEN));
label.setBackground(Color.WHITE);
label.setForeground(Color.BLUE);
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label);
}
public static void main(String[] args) {
new BoxLayoutLabelsTest("BoxLayoutLabelsTest").setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将以下行添加到
addLabel()
中:请参阅 如何使用 BoxLayout 获取完整示例。
稍后添加:
我找到了简单的解决方案:
这也有效,但是您使用 BorderLayout 的解决方案似乎更合适。
Add the following line into
addLabel()
:See How To Use BoxLayout for complete example.
Added later:
I've found straightforward solution:
This also works, but your solution with BorderLayout seems more appropriate.