【问题标题】:BorderLayout align边框布局对齐
【发布时间】:2015-04-12 13:44:40
【问题描述】:

我有一个带有 BorderLayoutJPanel。在这个 JPanelCenter 中,我有另一个带有 GridBagLayoutJPanel。我想在左上角的第二个 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);
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-03
      • 2014-02-02
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      • 2013-07-07
      • 1970-01-01
      • 2021-05-15
      • 2022-01-12
      相关资源
      最近更新 更多