如何使用 BoxLayout 使 JLabel 在面板中居中
How to center JLabel in a panel with BoxLayout
我是 Java 编程的新手,我有一个关于 BoxLayout 的问题。我无法使用 BoxLayout
将 JLabel 居中放置在面板中
我想要的是改变我现在得到的:
对此:
让标签完全位于面板的中间。
这是我的代码:
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test extends JFrame{
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JLabel label4;
private JLabel label5;
public Test(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initWidgets();
setVisible(true);
}
private void initWidgets(){
setPreferredSize(new Dimension(300, 300));
label1 = new JLabel("Label 1");
label2 = new JLabel("Label 2");
label3 = new JLabel("Label 3");
label4 = new JLabel("Label 4");
label5 = new JLabel("Label 5");
JPanel jpanel = new JPanel();
label1.setAlignmentX(CENTER_ALIGNMENT);
label2.setAlignmentX(CENTER_ALIGNMENT);
label3.setAlignmentX(CENTER_ALIGNMENT);
label4.setAlignmentX(CENTER_ALIGNMENT);
label5.setAlignmentX(CENTER_ALIGNMENT);
jpanel.setLayout(new BoxLayout(jpanel, BoxLayout.PAGE_AXIS));
jpanel.add(label1);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label2);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label3);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label4);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label5);
add(jpanel);
pack();
}
public static void main(String[] args) {
new Test();
}
}
尝试在 setAlignmentX
之后添加这个
label1.setHorizontalAlignment(SwingConstants.CENTER);
label2.setHorizontalAlignment(SwingConstants.CENTER);
label3.setHorizontalAlignment(SwingConstants.CENTER);
label4.setHorizontalAlignment(SwingConstants.CENTER);
label5.setHorizontalAlignment(SwingConstants.CENTER);
然后像这样在面板上添加标签:
jpanel.add(label1, BorderLayout.CENTER);
jpanel.add(label2, BorderLayout.CENTER);
jpanel.add(label3, BorderLayout.CENTER);
jpanel.add(label4, BorderLayout.CENTER);
jpanel.add(label5, BorderLayout.CENTER);
要垂直居中您需要在开始和结束处添加 "glue" 的组件:
jpanel.add(Box.createVerticalGlue());
jpanel.add(label1);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label2);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label3);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label4);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label5);
jpanel.add(Box.createVerticalGlue());
阅读 How to Use BoxLayout 上的 Swing 教程部分了解更多信息。
我是 Java 编程的新手,我有一个关于 BoxLayout 的问题。我无法使用 BoxLayout
将 JLabel 居中放置在面板中我想要的是改变我现在得到的:
对此:
让标签完全位于面板的中间。
这是我的代码:
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test extends JFrame{
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JLabel label4;
private JLabel label5;
public Test(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initWidgets();
setVisible(true);
}
private void initWidgets(){
setPreferredSize(new Dimension(300, 300));
label1 = new JLabel("Label 1");
label2 = new JLabel("Label 2");
label3 = new JLabel("Label 3");
label4 = new JLabel("Label 4");
label5 = new JLabel("Label 5");
JPanel jpanel = new JPanel();
label1.setAlignmentX(CENTER_ALIGNMENT);
label2.setAlignmentX(CENTER_ALIGNMENT);
label3.setAlignmentX(CENTER_ALIGNMENT);
label4.setAlignmentX(CENTER_ALIGNMENT);
label5.setAlignmentX(CENTER_ALIGNMENT);
jpanel.setLayout(new BoxLayout(jpanel, BoxLayout.PAGE_AXIS));
jpanel.add(label1);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label2);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label3);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label4);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label5);
add(jpanel);
pack();
}
public static void main(String[] args) {
new Test();
}
}
尝试在 setAlignmentX
label1.setHorizontalAlignment(SwingConstants.CENTER);
label2.setHorizontalAlignment(SwingConstants.CENTER);
label3.setHorizontalAlignment(SwingConstants.CENTER);
label4.setHorizontalAlignment(SwingConstants.CENTER);
label5.setHorizontalAlignment(SwingConstants.CENTER);
然后像这样在面板上添加标签:
jpanel.add(label1, BorderLayout.CENTER);
jpanel.add(label2, BorderLayout.CENTER);
jpanel.add(label3, BorderLayout.CENTER);
jpanel.add(label4, BorderLayout.CENTER);
jpanel.add(label5, BorderLayout.CENTER);
要垂直居中您需要在开始和结束处添加 "glue" 的组件:
jpanel.add(Box.createVerticalGlue());
jpanel.add(label1);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label2);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label3);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label4);
jpanel.add(Box.createRigidArea(new Dimension(0, 10)));
jpanel.add(label5);
jpanel.add(Box.createVerticalGlue());
阅读 How to Use BoxLayout 上的 Swing 教程部分了解更多信息。