我正在尝试为我的登录对话框构建一个状态栏,但标签没有与状态面板的左侧对齐。这是我的代码。
public class LoginDialog extends JDialog {
private static final long serialVersionUID = 1L;
protected JLabel lblTopSpace = null;
protected JPanel loginPanel = null;
protected JPanel statusPanel = null;
public LoginDialog(String title) {
super((Dialog)null);
this.setTitle(title);
Initialize();
}
protected void Initialize() {
lblTopSpace = new JLabel("Login into Bookyard");
lblTopSpace.setForeground(this.getBackground());
loginPanel = new LoginPanel();
statusPanel = new JPanel();
statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
statusPanel.setSize(this.getWidth(), 50);
JLabel lblStatus = new JLabel("Status");
lblStatus.setFont(new Font("Verdana", Font.PLAIN, 12));
lblStatus.setHorizontalAlignment(SwingConstants.LEFT);
statusPanel.add(lblStatus);
this.setLayout(new BorderLayout());
Container container = this.getContentPane();
container.add(lblTopSpace, BorderLayout.NORTH);
container.add(loginPanel, BorderLayout.CENTER);
container.add(statusPanel, BorderLayout.SOUTH);
this.pack();
}
}这是它目前的样子。

我遗漏了什么?
发布于 2016-08-26 18:08:13
您的标签位于对话框内容窗格内的面板内。因此,标签使用其父面板的布局进行管理。但是您没有为它设置任何特定的布局,那么它就是一个FlowLayout,然后您的标签就会在其中居中显示文本所需的最小尺寸。然后标签在自己的区域中左对齐,但此标签在面板中居中。
更改面板的布局以使标签在其中延伸(添加BorderLayout并将标签设置在面板的北侧、中部或南侧),或者删除似乎无用的面板(并让标签在内容窗格的南侧延伸)。
statusPanel.setLayout(new BorderLayout());
statusPanel.add(lblStatus,BorderLayout.SOUTH);或
container.add(lblStatus,BorderLayout.SOUTH);https://stackoverflow.com/questions/39163624
复制相似问题