gpt4 book ai didi

java - 如何使用元素的中心将 BoxLayout 中的元素居中?

转载 作者:IT老高 更新时间:2023-10-28 21:14:46 26 4
gpt4 key购买 nike

我使用 outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.Y_AXIS)); 然后将元素(例如 JLabels、JButtons)添加到 outputPanel。例如:outputPanel.add(submitButton);.

我看到所有添加的元素都是“居中”的。这很好,因为我确实希望我的元素位于中心。当我写“中心”时,我的意思是“左右距离相等”。但问题是元素的左侧部分被放入了中心。我希望将元素的中心放入中心。我怎样才能得到这种行为?

最佳答案

问题可以通过使用myLabel.setAlignmentX(Component.CENTER_ALIGNMENT);来解决。它适用于 JLabelJButtonJRadioButton

关于java - 如何使用元素的中心将 BoxLayout 中的元素居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2560784/

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