gpt4 book ai didi

java - 如何在java布局中左对齐标签?

转载 作者:行者123 更新时间:2023-12-02 09:35:42 26 4
gpt4 key购买 nike

我正在尝试将标签向左对齐。布局管理器根本无法直观地或按预期工作。

我浏览了互联网,但没有任何内容解释我需要什么。或者我发现的例子根本不起作用。例如,我已经设置了所有对齐参数,但没有任何变化。

请帮忙。

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);

“标签未向左对齐。”

最佳答案

当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

设置所有子组件的 X 对齐方式

第一个选项是在添加到父组件的子组件上使用 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);
}
}

关于java - 如何在java布局中左对齐标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57538179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com