グリッドでコンテナーを「いっぱいにする」、または 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 ) を投稿してください。