【问题标题】:Using BoxLayout as vertical FlowLayout to hold JPanel使用 BoxLayout 作为垂直 FlowLayout 来容纳 JPanel
【发布时间】:2013-09-15 03:46:34
【问题描述】:

我想要一个垂直的FlowLayout 来托管我的JPanels。很多人建议使用BoxLayout。但是,我意识到它的行为与FlowLayout

并不完全相同

流式布局

带 Y 轴的 BoxLayout

如您所见,在FlowLayout 中,当我拉伸父面板的宽度时,其子面板的宽度保持不变。

但是,在BoxLayout 中,当我拉伸父面板的高度时,其子面板的高度发生了变化!。这似乎与 1 列 2 行 GridLayout 具有相似的行为。这不是我想要的。

有什么办法可以防止这种情况发生吗?

我尝试在父面板的顶部和底部添加垂直填充。

new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));

但这并没有多大帮助。当我改变父母的高度时,我的 2 个子面板的高度仍然会延伸。

【问题讨论】:

    标签: java swing layout-manager flowlayout boxlayout


    【解决方案1】:

    【讨论】:

      【解决方案2】:
      • 查看 BoxLayout(接受最小、最大和首选大小,然后根据该值调整大小)的工作原理,

      • 与 FlowLayout 相比(仅接受 PreferredSize,子级不能随容器调整大小)

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      
      public class BoxStructAndJComponents {
      
          private JFrame frame;
          private JPanel intro;
          private JPanel name;
      
          public BoxStructAndJComponents() {
              frame = new JFrame("JFrame");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JComponent newContentPane = createUI();
              newContentPane.setOpaque(true);
              frame.setContentPane(newContentPane);
              frame.pack();
              frame.setVisible(true);
          }
      
          private JPanel createUI() {
              intro = new JPanel() {
                  @Override
                  public Dimension getMinimumSize() {
                      return new Dimension(100, 100);
                  }
      
                  @Override
                  public Dimension getPreferredSize() {
                      return new Dimension(100, 100);
                  }
      
                  @Override
                  public Dimension getMaximumSize() {
                      return new Dimension(100, 100);
                  }
              };
              intro.setBackground(Color.red);
              //intro.setLabelFor(name);
              name = new JPanel() {
                  @Override
                  public Dimension getMinimumSize() {
                      return new Dimension(100, 100);
                  }
      
                  @Override
                  public Dimension getPreferredSize() {
                      return new Dimension(100, 100);
                  }
      
                  @Override
                  public Dimension getMaximumSize() {
                      return new Dimension(100, 100);
                  }
              };
              name.setBackground(Color.blue);
              final JButton button = new JButton("Pick a new name...");
              button.addActionListener(new ActionListener() {
                  @Override
                  public void actionPerformed(ActionEvent e) {
                  }
              });
              JPanel panel = new JPanel();
              panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
              panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 10, 20));
              intro.setAlignmentX(JComponent.CENTER_ALIGNMENT);
              name.setAlignmentX(JComponent.CENTER_ALIGNMENT);
              button.setAlignmentX(JComponent.CENTER_ALIGNMENT);
              panel.add(intro);
              //panel.add(Box.createVerticalStrut(5));
              panel.add(Box.createHorizontalStrut(5));
              panel.add(name);
              panel.add(Box.createRigidArea(new Dimension(150, 10)));
              panel.add(button);
              return panel;
          }
      
          public static void main(String[] args) {
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  @Override
                  public void run() {
                      BoxStructAndJComponents listDialogRunner = new BoxStructAndJComponents();
                  }
              });
          }
      }
      

      【讨论】:

        【解决方案3】:

        实现此行为的一种简单灵活的方法是使用GridBagLayout

        import java.awt.GridBagConstraints;
        import java.awt.GridBagLayout;
        
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        import javax.swing.JPanel;
        import javax.swing.SwingUtilities;
        
        public class TestGridBagLayout {
        
            protected void initUI1() {
                final JFrame frame = new JFrame("Grid bag layout");
                frame.setTitle(TestGridBagLayout.class.getSimpleName());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel panel = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                // gbc.weighty = 1.0; Uncomment this line if you want the labels to spread vertically
                for (int i = 0; i < 10; i++) {
                    panel.add(new JLabel("Label " + (i + 1)), gbc);
                }
                frame.add(panel);
                frame.pack();
                frame.setVisible(true);
            }
        
            public static void main(String[] args) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        TestGridBagLayout test = new TestGridBagLayout();
                        test.initUI1();
                    }
                });
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-06-10
          • 2013-06-24
          • 1970-01-01
          • 2013-05-26
          • 2014-10-31
          • 2015-05-06
          • 2012-06-18
          相关资源
          最近更新 更多