1

Take a look at this image:

enter image description here

As you can see, I have a JSeparator between my "Auto Refreshing" JCheckBox and my "Show Column" menu, and my "Show Column" menu is wanting to be as far right as possible. Why is it not aligning itself to the left, like everything else before the JSeparator? And I can't seem to make it do so, here is my current code:

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

2 回答 2

2

教程可能会有所帮助。一个报价:

默认情况下,大多数组件具有中心 X 和 Y 对齐方式。但是,按钮、组合框、标签和菜单项具有不同的默认 X 对齐值:LEFT_ALIGNMENT。

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

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

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

于 2015-08-11T05:59:30.023 回答
0

问题在于 JSeparator 的大小,它希望尽可能多地占用水平空间。所以,我的解决方案是限制它的大小,使其最大只能是一个像素宽:

JSeparator menuSep = new JSeparator(javax.swing.SwingConstants.VERTICAL);
menuSep.setMaximumSize(new java.awt.Dimension(1, 1000));
menuBar.add(menuSep);
于 2015-08-12T22:03:25.993 回答