簡體   English   中英

BorderLayout對齊

[英]BorderLayout align

我有一個帶有BorderLayoutJPanel 在此JPanel中心 ,我還有另一個GridBagLayout的 JPanel 我想在第二個JPanel中從左上角垂直添加一些JLabel 我需要BorderLayout,因為我需要在北部區域添加標題。

我該如何實現?

在此處輸入圖片說明

您實際上並不需要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);
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM