【问题标题】:How to adjust the grid size after resizing the frame?调整框架大小后如何调整网格大小?
【发布时间】:2011-05-28 12:35:08
【问题描述】:

我想知道以下几点:我有一个 MainWindow 组件(其中包含一个框架 (JFrame))和几个其他 JPanel。其中一个 JPanel,假设 gridPanel 使用 gridLayout 作为 LayoutManager。现在我的问题是我想在调整窗口大小后调整(设置行的大小/设置列的大小)。有人可以告诉我如何实现在调整框架大小后可以触发的操作,因为我不熟悉所涉及的侦听器。

这应该是在最“标准”的编码实践中完成的。感谢您的回复和解答!

【问题讨论】:

    标签: java swing resize frame grid-layout


    【解决方案1】:

    如果你想让你的网格“填满”一个容器,或者填满 JFrame,那么关键是使用适当的布局管理器来保存使用 GridLayout 的容器。例如,如果您将使用 GridLayout 的容器添加到另一个使用 FlowLayout 的容器中,那么使用 GridLayout 的容器在其容纳容器改变大小时不会改变大小。但是,如果您将使用 GridLayout 的容器添加到另一个使用 BorderLayout 的容器及其 ​​BorderLayout.CENTER 位置,则使用 GridLayout 的容器将随着使用 BorderLayout 的父容器调整大小而调整大小。

    例子:

    import java.awt.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ExpandingGrid extends JPanel {
       private static final int GAP = 5;
    
       public ExpandingGrid() {
    
          // create a BorderLayout-using JPanel
          JPanel borderLayoutPanel = new JPanel(new BorderLayout());
          borderLayoutPanel.setBorder(BorderFactory.createTitledBorder("BorderLayout Panel"));
          borderLayoutPanel.add(createGridPanel(), BorderLayout.CENTER); // add a Grid to it
    
          // create a FlowLayout-using JPanel
          JPanel flowLayoutPanel = new JPanel(new FlowLayout());
          flowLayoutPanel.setBorder(BorderFactory.createTitledBorder("FlowLayout Panel"));
          flowLayoutPanel.add(createGridPanel()); // add a grid to it
    
          // set up the main JPanel 
          setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
          setLayout(new GridLayout(1, 0, GAP, 0)); // grid with 1 row
          // and add the borderlayout and flowlayout using JPanels to it
          add(borderLayoutPanel);
          add(flowLayoutPanel);
       }
    
       // create a JPanel that holds a bunch of JLabels in a GridLayout
       private JPanel createGridPanel() {
          int rows = 5;
          int cols = 5;
          JPanel gridPanel = new JPanel(new GridLayout(rows, cols));
          for (int i = 0; i < rows; i++) {
             for (int j = 0; j < cols; j++) {
                // create the JLabel that simply shows the row and column number
                JLabel label = new JLabel(String.format("[%d, %d]", i, j),
                         SwingConstants.CENTER);
                // give the JLabel a border
                label.setBorder(BorderFactory.createEtchedBorder());
                gridPanel.add(label); // add to the GridLayout using JPanel
             }
          }
          return gridPanel;
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("ExpandingGrid");
          frame.getContentPane().add(new ExpandingGrid());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    

    另外,如果这没有帮助,那么您可能希望详细说明您的问题和邮政编码,最好是SSCCE

    【讨论】:

    • 我理解那部分,但是我想要以下内容:我想要一个简单的添加命令,它将我的按钮放置在网格中。但是,当我的框架变大时,我想要更多的列,否则组件之间的空间会增加。这就是为什么我只想调整列。我还没有找到任何其他合适的布局管理器,也许你可以推荐一个? (需要布局管理器的简单性,因为我不想过度使用(例如使用网格包布局)或其他东西;但是,如果网格包布局足够(没有太多麻烦,我会尝试)。
    • @Anonymous:我建议您创建一个SSCCE,就像我在上面创建的那样,以更好地说明您的问题。
    • @Anonymous:我的解决方案是固定数量的列和行(这是我认为您的原始帖子要求的)。另外,请告诉我们您想用这个 GUI 做什么,为什么要拥有这些属性,因为也许有比您尝试的更好的方法来解决这个问题,例如使用 JTable。
    【解决方案2】:

    这就是我只想调整列的原因。

    也许Wrap Layout 就是您要找的。​​p>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      相关资源
      最近更新 更多