gpt4 book ai didi

java - JMenuBar 内的组件想要在 JSeperator 之后最右边对齐?

转载 作者:行者123 更新时间:2023-11-30 03:18:29 26 4
gpt4 key购买 nike

看看这张图片:

enter image description here

如您所见,我的“自动刷新”JCheckBox 和“显示列”菜单之间有一个 JSeparator,并且我的“显示列”菜单希望尽可能位于右侧。为什么它不像 JSeparator 之前的其他所有内容一样向左对齐?我似乎无法做到这一点,这是我当前的代码:

JCheckBox pulling = new JCheckBox("Auto Refreshing");
...
menuBar.add(pulling);

menuBar.add(new javax.swing.JSeparator(javax.swing.SwingConstants.VERTICAL));

JMenu showMenu = new JMenu("Show Column");
showMenu.setAlignmentX(Component.LEFT_ALIGNMENT);
menuBar.add(showMenu);

最佳答案

这个tutorial可能会有帮助。引用:

By default, most components have center X and Y alignment. However, buttons, combo boxes, labels, and menu items have a different default X alignment value: LEFT_ALIGNMENT.

所以你可以看到放置逻辑不同,换句话说,不要指望它。但是,我不知道为什么你的手动向左对齐不起作用。最有可能的问题是你最后一个菜单的大小。你能做的就是使用 glue as filler因为 JMenuBar 有一个 BoxLayout

menuBar.add(showMenu);
menuBar.add(Box.createHorizontalGlue());

这个不可见的空间将被添加到菜单的末尾,并将其之前的组件推向左侧。

关于java - JMenuBar 内的组件想要在 JSeperator 之后最右边对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31927758/

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