我正在尝试将标签向左对齐。 布局管理器根本无法直观或按预期工作。
我搜寻了Internet,但没有任何资料可以解释我的需求。 或我发现的例子根本行不通。例如,我已经设置了所有对齐参数,并且没有任何变化。
请帮助。
Labels do not left align.
“我已经尝试了所有类型的布局和所有对齐设置。”
JPanel p0=new JPanel();
p0.setLayout(new BoxLayout(p0,BoxLayout.Y_AXIS));
p0.setBorder(new EmptyBorder(10,10,10,10));
p0.setAlignmentX(0);
JPanel p0a=new JPanel();
p0a.setLayout(new BoxLayout(p0a,BoxLayout.X_AXIS));
JLabel l=new JLabel("Label 1");
p0a.add(l);
p0a.setAlignmentX(0);
l.setAlignmentX(0);
l.setHorizontalAlignment(0);
p0.add(p0a);
JPanel p0b=new JPanel();
p0b.setLayout(new BoxLayout(p0b,BoxLayout.Y_AXIS));
p0b.add(new JLabel("Label 1"));
p0.add(p0b);
JPanel p0c=new JPanel();
p0c.setLayout(new BoxLayout(p0c,BoxLayout.Y_AXIS));
p0c.add(new JLabel("Label 1"));
p0.add(p0c);
"create labels"
JPanel p1=new JPanel();
p1.setLayout(new BoxLayout(p1,BoxLayout.X_AXIS));
p1.add(new JLabel("Label 4a"));
p1.add(new JLabel("Label 4b"));
p1.add(new JLabel("Label 4c"));
p1.add(new JLabel("Label 4d"));
p0.add(p1);
p0.add(new JLabel("Label 5"));
p0.add(new JLabel("Label 6"));
p0.add(new JLabel("Label 7"));
JDialog jd=new JDialog();
jd.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent _e){jd.setVisible(false);}});
jd.setLayout(new BoxLayout(jd.getContentPane(),BoxLayout.Y_AXIS));
Container c=jd.getContentPane();
jd.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
c.add(p0);
jd.pack();
jd.setVisible(true);
“标签未向左对齐。”
答案 0 :(得分:1)
当X对齐设置为LEFT_ALIGNMENT
时,所有添加到父级的组件都将左对齐。 RIGHT_ALIGNMENT
也是如此。当混合行为不同时。
要最小化解决问题,只需将设置X对齐方式添加到水平添加了多个标签的面板上即可。
JPanel p1=new JPanel();
p1.setLayout(new BoxLayout(p1,BoxLayout.X_AXIS));
p1.add(new JLabel("Label 4a"));
p1.add(new JLabel("Label 4b"));
p1.add(new JLabel("Label 4c"));
p1.add(new JLabel("Label 4d"));
p1.setAlignmentX(Component.LEFT_ALIGNMENT);
有很多选择-始终使用简单的X对齐,而另一个复杂的选择是使用Box
。
第一种选择是在添加到父级的子级组件上使用setAlignmentX(Component.LEFT_ALIGNMENT)
。
private static void addAndLeftAlign(JComponent parent, JComponent child) {
child.setAlignmentX(Component.LEFT_ALIGNMENT);
parent.add(child);
}
这是您修改的代码:
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class LeftAlignLabels57538179 {
public static void main(String[] args) {
JPanel p0 = new JPanel();
//p0.setBackground(Color.ORANGE);
p0.setLayout(new BoxLayout(p0, BoxLayout.Y_AXIS));
p0.setBorder(new EmptyBorder(10, 10, 10, 10));
p0.setAlignmentX(0);
JPanel p0a = new JPanel();
p0a.setLayout(new BoxLayout(p0a, BoxLayout.X_AXIS));
JLabel l = new JLabel("Label 1");
p0a.add(l);
//p0a.setAlignmentX(0);
//l.setAlignmentX(0);
//l.setHorizontalAlignment(0);
//p0.add(p0a);
addAndLeftAlign(p0, p0a);
JPanel p0b = new JPanel();
p0b.setLayout(new BoxLayout(p0b, BoxLayout.Y_AXIS));
p0b.add(new JLabel("Label 1"));
//p0.add(p0b);
addAndLeftAlign(p0, p0b);
JPanel p0c = new JPanel();
p0c.setLayout(new BoxLayout(p0c, BoxLayout.Y_AXIS));
p0c.add(new JLabel("Label 1"));
//p0.add(p0c);
addAndLeftAlign(p0, p0c);
//"create labels"
JPanel p1 = new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
p1.add(new JLabel("Label 4a"));
p1.add(new JLabel("Label 4b"));
p1.add(new JLabel("Label 4c"));
p1.add(new JLabel("Label 4d"));
//p0.add(p1);
addAndLeftAlign(p0, p1);
addAndLeftAlign(p0, new JLabel("Label 5"));
addAndLeftAlign(p0, new JLabel("Label 6"));
addAndLeftAlign(p0, new JLabel("Label 7"));
JDialog jd = new JDialog();
jd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent _e) {
jd.setVisible(false);
}
});
jd.setLayout(new BoxLayout(jd.getContentPane(), BoxLayout.Y_AXIS));
Container c = jd.getContentPane();
jd.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
c.add(p0);
jd.pack();
jd.setVisible(true);
}
private static void addAndLeftAlign(JComponent parent, JComponent child) {
child.setAlignmentX(Component.LEFT_ALIGNMENT);
parent.add(child);
}
}
要左对齐另一个选项,以通过以下方式使用Box
一个非常轻便的组件:
private static void addAndLeftAlign(JComponent parent, JComponent child){
Box b = Box.createHorizontalBox();
b.add(child);
b.add( Box.createHorizontalGlue() );
parent.add(b);
}
这是您修改的代码:
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class LeftAlignLabels57538179 {
public static void main(String[] args) {
JPanel p0=new JPanel();
p0.setLayout(new BoxLayout(p0,BoxLayout.Y_AXIS));
p0.setBorder(new EmptyBorder(10,10,10,10));
p0.setAlignmentX(0);
JPanel p0a=new JPanel();
p0a.setLayout(new BoxLayout(p0a,BoxLayout.X_AXIS));
JLabel l=new JLabel("Label 1");
p0a.add(l);
p0a.setAlignmentX(0);
l.setAlignmentX(0);
l.setHorizontalAlignment(0);
//p0.add(p0a);
addAndLeftAlign(p0, p0a);
JPanel p0b=new JPanel();
p0b.setLayout(new BoxLayout(p0b,BoxLayout.Y_AXIS));
p0b.add(new JLabel("Label 1"));
//p0.add(p0b);
addAndLeftAlign(p0, p0b);
JPanel p0c=new JPanel();
p0c.setLayout(new BoxLayout(p0c,BoxLayout.Y_AXIS));
p0c.add(new JLabel("Label 1"));
//p0.add(p0c);
addAndLeftAlign(p0, p0c);
//"create labels"
JPanel p1=new JPanel();
p1.setLayout(new BoxLayout(p1,BoxLayout.X_AXIS));
p1.add(new JLabel("Label 4a"));
p1.add(new JLabel("Label 4b"));
p1.add(new JLabel("Label 4c"));
p1.add(new JLabel("Label 4d"));
//p0.add(p1);
addAndLeftAlign(p0, p1);
addAndLeftAlign(p0, new JLabel("Label 5"));
addAndLeftAlign(p0, new JLabel("Label 6"));
addAndLeftAlign(p0, new JLabel("Label 7"));
JDialog jd=new JDialog();
jd.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent _e){jd.setVisible(false);}});
jd.setLayout(new BoxLayout(jd.getContentPane(),BoxLayout.Y_AXIS));
Container c=jd.getContentPane();
jd.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
c.add(p0);
jd.pack();
jd.setVisible(true);
}
private static void addAndLeftAlign(JComponent parent, JComponent child){
Box b = Box.createHorizontalBox();
b.add(child);
b.add( Box.createHorizontalGlue() );
parent.add(b);
}
}