【问题标题】:BorderLayout align边框布局对齐
【发布时间】:2015-04-12 13:44:40
【问题描述】:
我有一个带有 BorderLayout 的 JPanel。在这个 JPanel 的 Center 中,我有另一个带有 GridBagLayout 的 JPanel。我想在左上角的第二个 JPanel 中垂直添加一些 JLabels。我需要 BorderLayout 因为我需要在 North 区域添加我的标题。
我怎样才能做到这一点?
【问题讨论】:
标签:
java
swing
layout-manager
gridbaglayout
border-layout
【解决方案1】:
你真的不需要GridBagLayout,你可以使用更简单的BoxLayout:
public class Popup {
public static void main(String[] args) {
JFrame window = new JFrame("Title");
window.add(new JLabel("North", JLabel.CENTER), BorderLayout.NORTH);
window.add(new JLabel("South", JLabel.CENTER), BorderLayout.SOUTH);
window.add(new JLabel("West"), BorderLayout.WEST);
window.add(new JLabel("East"), BorderLayout.EAST);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
centerPanel.add(new JLabel("Here"));
centerPanel.add(new JLabel("Here"));
centerPanel.add(new JLabel("Here"));
centerPanel.add(new JLabel("Here"));
window.add(centerPanel, BorderLayout.CENTER);
window.setSize(600, 400);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}