调整框架大小后如何调整网格大小?

发布于 2024-11-09 22:45:06 字数 237 浏览 6 评论 0原文

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

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

I was wondering the following: I have a MainWindow component (which contains a frame (JFrame)) and several other JPanels. Where one JPanel, let's say gridPanel uses the gridLayout as LayoutManager. Now my problem is that I want to adjust (set the size of rows / set the size of columns) after the window has been resized. Can someone tell me how I can achieve actions that can be triggered after resizing the frame as I am not familiar with the listeners involved.

It should be the done on the most "standard" coding practices. Thank you for your response and answers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱情眠于流年 2024-11-16 22:45:06

如果您希望网格“填满”一个容器,或者填满 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

If you want your grid to "fill up" a container, or fill up the JFrame, then key is to use proper layout managers to hold the GridLayout-using container. For instance if you add the GridLayout-using container to another container that uses FlowLayout, then the GridLayout-using container will not change size if its holding container changes size. However if you add the GridLayout-using container to another container that uses BorderLayout and to its BorderLayout.CENTER position, then the GridLayout-using container will resize as the BorderLayout-using parent container resizes.

Example:

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();
         }
      });
   }
}

Also, if this is not helpful, then you may wish to elaborate more on your problem and post code, preferably an SSCCE.

旧情勿念 2024-11-16 22:45:06

这就是为什么我只想调整列。

也许 Wrap Layout 就是您正在寻找的。

That's why I wanted to only adjust the columns.

Maybe the Wrap Layout is what you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文