1

私は次のことを考えていました: MainWindow コンポーネント (フレーム (JFrame) を含む) と他のいくつかの JPanel があります。1 つの JPanel で、gridPanel が gridLayout を LayoutManager として使用するとします。今私の問題は、ウィンドウのサイズが変更された後に調整(行のサイズを設定/列のサイズを設定)したいということです。関連するリスナーに慣れていないため、フレームのサイズを変更した後にトリガーできるアクションを実現する方法を教えてください。

これは、最も「標準的な」コーディング方法で行う必要があります。ご回答、ご回答ありがとうございます!

4

2 に答える 2

2

グリッドでコンテナーを「いっぱいにする」、または JFrame をいっぱいにする場合は、適切なレイアウト マネージャーを使用して GridLayout を使用するコンテナーを保持することが重要です。たとえば、GridLayout を使用するコンテナーを FlowLayout を使用する別のコンテナーに追加する場合、保持しているコンテナーのサイズが変更されても、GridLayout を使用するコンテナーのサイズは変更されません。ただし、GridLayout を使用するコンテナーを、BorderLayout を使用する別のコンテナーとその BorderLayout.CENTER 位置に追加すると、BorderLayout を使用する親コンテナーのサイズが変更されると、GridLayout を使用するコンテナーのサイズが変更されます。

例:

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 ) を投稿してください。

于 2011-05-28T12:39:56.557 に答える
0

そのため、列のみを調整したかったのです。

ラップ レイアウトが探しているものかもしれません。

于 2011-05-28T14:56:23.440 に答える