[英]Java Swing BoxLayout ignoring AlignmentX
In the code below, by calling setAlignmentX
with Component.LEFT_ALIGNMENT
I expected to get a left aligned label over a centered slider. 在下面的代码中,通过使用Component.LEFT_ALIGNMENT
调用setAlignmentX
,我希望在居中的滑块上获得左对齐标签。 For some reason the label is also centered, seemingly regardless of what value is passed to setAlignmentX. 由于某种原因,标签也是居中的,看起来无论传递给setAlignmentX的值是什么。
What value must I pass to setAlignmentX to get it left aligned? 我必须将什么值传递给setAlignmentX以使其保持对齐?
package myjava;
import java.awt.Component;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
public class LayoutTest {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("BoxLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create left aligned label over centered column
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JLabel label = new JLabel("test");
label.setAlignmentX(Component.LEFT_ALIGNMENT);
contentPane.add(label);
contentPane.add(new JSlider());
frame.pack();
frame.setVisible(true);
}
});
}
}
Basically, you can't have different alignments in BoxLayout, from How To Use BoxLayout 基本上,你不能在BoxLayout中使用不同的路线, 如何使用BoxLayout
In general, all the components controlled by a top-to-bottom BoxLayout object should have the same X alignment. 通常,由顶部到底部BoxLayout对象控制的所有组件应具有相同的X对齐。
Edit 编辑
Typically, it's not documented which default alignment a component type has (JSlider is centered by default, me incorrectly thought that a JLabel were centered while it is left-aligned ;-) One option is to keep a list somewhere (dooooh...), another is to simply force them all to the same alignment on adding. 通常,没有记录组件类型具有哪个默认对齐(JSlider默认居中,我错误地认为JLabel在左对齐时居中;-)一个选项是将列表保留在某处(dooooh ...) ,另一个是在添加时简单地强制它们全部达到相同的对齐方式。
Or use a third-party layoutManager, which doesn't have this rather unintuitve (for me) mix-in of layout and alignment. 或者使用第三方layoutManager,它没有这种相当不合适的(对我来说)布局和对齐的混合。
BoxLayout have strange behavior. BoxLayout有奇怪的行为。 Try to use GridBagLayout
instead: 尝试使用GridBagLayout
代替:
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
public class Aligment {
public static void main(String[] args) {
final JPanel root = new JPanel(new GridBagLayout());
root.setPreferredSize(new Dimension(500, 400));
root.add(new JLabel("LEFT"), new GridBagConstraints() {{
gridx = 0;
gridy = 0;
anchor = PAGE_START;
}});
root.add(new JLabel("CENTER"), new GridBagConstraints() {{
gridx = 1;
gridy = 1;
anchor = CENTER;
weightx = 1.0; // fill Width
}});
root.add(new JLabel("RIGHT"), new GridBagConstraints() {{
gridx = 2;
gridy = 2;
anchor = LINE_END;
}});
// hack: Push all rows to Top
root.add(Box.createVerticalGlue(), new GridBagConstraints() {{
gridx = 0;
gridy = 3;
weighty = 1.0; // fill Height
}});
new JFrame() {
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(root);
pack();
setLocationRelativeTo(null);;
}
}.setVisible(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.