【发布时间】:2020-10-02 20:26:24
【问题描述】:
这似乎是一件简单的事情,但我无法让它发挥作用。
我有一个BorderLayout。我想将顶部用作标题栏。我想添加一个带有标签、按钮和其他组件的JPanel。但是,边框布局的PAGE_START 部分不会左对齐面板。这是我尝试设置对齐方式的地方。
我注意到当我没有在边框布局中添加面板时,直接写JLabel,默认情况下它是左对齐的。
但这不是我想要的,因为我计划将BoxLayout.X_AXIS 水平放置在BorderLayout.PAGE_START 标题区域中。似乎是合理的做法?
静态方法的Container 窗格参数只是主JFrame 上的单个面板。
public static void addComponentsToPane(Container pane)
{
JLabel jlabel = new JLabel("I want to left align this inside a JPanel");
// Doesn't work: jlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel jpanel = new JPanel();
//Doesn't work: jlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
jpanel.add(jlabel);
pane.add(jpanel, BorderLayout.PAGE_START);
// Other parts of the BoxLayout (works fine)
JButton button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);
button = new JButton("Button 3 (LINE_START)");
pane.add(button, BorderLayout.LINE_START);
button = new JButton("Long-Named Button 4 (PAGE_END)");
pane.add(button, BorderLayout.PAGE_END);
button = new JButton("5 (LINE_END)");
pane.add(button, BorderLayout.LINE_END);
}
即使我告诉面板左对齐标签,它也不会显示为左对齐。
有谁知道我做错了什么?
【问题讨论】:
标签: java swing alignment layout-manager boxlayout