【发布时间】:2019-05-06 03:25:58
【问题描述】:
我需要将组件与JTextArea 组件完全对齐,我目前使用的是BoxLayout,并且我已经使用了setAlignmentX 和setHorizontalAlignment 到LEFT,但它不起作用。在这里,我上传了一张图片以更清楚地表达我的意思。例如查看“+ Pla PLAMARC”,它显然与文本区域组件不对齐。
现在是代码:
//Declarations
private JLabel nomPla;
private JTextArea infoPla;
private JScrollPane textAreaScroll;
//Inside the constructor
nomPla = new JLabel();
infoPla = new JTextArea(2, 50);
textAreaScroll = new JScrollPane(infoPla);
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
nomPla.setAlignmentX(Component.LEFT_ALIGNMENT);
nomPla.setHorizontalAlignment(nomPla.LEFT);
textAreaScroll.setAlignmentX(Component.CENTER_ALIGNMENT);
this.setBorder(new EmptyBorder(new Insets(25, 25, 25, 25)));
this.add(nomPla, BorderLayout.NORTH);
this.add(textAreaScroll, BorderLayout.NORTH); //Orientacions
我明确告诉nomPla 在左侧,但这与JTextArea 不同。
那么这是怎么做到的呢?
【问题讨论】:
-
“那是怎么做的?” 你现在怎么样了?为了尽快获得更好的帮助,edit 添加minimal reproducible example 或Short, Self Contained, Correct Example。
-
使用一个borderLayout并将每个JTextArea组件放在你想要的每个部分(即左边)。
-
@AndrewThompson 已编辑...
-
@Marc43,
already edited- 这不是“MCVE”。我们无法编译该代码并进行测试以查看您描述的行为。
标签: java swing alignment layout-manager boxlayout