gpt4 book ai didi

java - 对齐所有面板组件ja​​va

转载 作者:行者123 更新时间:2023-11-29 06:21:43 25 4
gpt4 key购买 nike

我在 java 中使用 BoxLayout 布局管理器,并对齐了一堆组件:

myLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
myTextBox.setAlignmentX(Component.LEFT_ALIGNMENT);
myButton.setAlignmentX(Component.LEFT_ALIGNMENT);
...

我有很多组件,这似乎有点过头了。有没有简写的方法?

我尝试了以下方法,但是 setAlignmentX 不是 Component 内部的方法吗?

for (Component c : personPanel.getComponents()) {
c.setAlignmentX(Component.LEFT_ALIGNMENT);
}

最佳答案

setAlignmentXJComponent 中定义.

您可以在检查后转换:

for (Component c : personPanel.getComponents()) {
if(c instanceof JComponent) {
((JComponent)c).setAlignmentX(Component.LEFT_ALIGNMENT);
}
}

如果您嵌套了组件,则可能需要从中创建一个递归方法。

关于java - 对齐所有面板组件ja​​va,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2753243/

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