简体   繁体   中英

BorderLayout align

I have a JPanel with a BorderLayout . In the Center of this JPanel I have another JPanel with GridBagLayout . I want to add vertically in the second JPanel some JLabels from top left corner. I need BorderLayout because I need to add my title in the North area.

How can I achieve this?

在此处输入图片说明

You don't really need the GridBagLayout , you can use easier 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);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM