GridLayout中JPanel之间的差距

时间:2013-02-04 18:04:51

标签: java swing awt layout-manager grid-layout

下面的代码生成九个单独的JPanel,其中有9个按钮。使用GridLayout将九个JPanel排列到基本JPanel上。然后使用Border Layout将此基础JPanel放置在ContentPane上。我正在为JButtons和每个单独的JPanel使用边框来清楚地定义它们的分离。每个JPanel内部的JButtons看起来很好但是JPanels之间存在间隙,导致双线的出现让我感到害怕。我已经尝试将每个单独的JPanel直接添加到ContentPane上但这没有任何作用。完全难过,想知道是否有人有任何建议?

P.S。我知道重复代码的大量需要以一种糟糕的方式重新分解它,这只是出于模拟的目的。

谢谢,

import java.awt.*;
import javax.swing.*;
import javax.swing.UIManager.*;

public class View1 extends JFrame
{
    private JPanel      basePanel;
    private JPanel      childPanel1;
    private JPanel      childPanel2;
    private JPanel      childPanel3;
    private JPanel      childPanel4;
    private JPanel      childPanel5;
    private JPanel      childPanel6;
    private JPanel      childPanel7;
    private JPanel      childPanel8;
    private JPanel      childPanel9;
    private int         row = 3;
    private int         column = 3;
    private JButton[][] squares1;
    private JButton[][] squares2;
    private JButton[][] squares3;
    private JButton[][] squares4;
    private JButton[][] squares5;
    private JButton[][] squares6;
    private JButton[][] squares7;
    private JButton[][] squares8;
    private JButton[][] squares9;
    private Font        numberFont;

public View1()
{
    setSize(400,400);
    setTitle("2013");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    basePanel  = new JPanel();
    basePanel.setLayout(new GridLayout(row, column, 0, 0));
    numberFont = new Font("sansserif", Font.BOLD, 32);

    childPanel1Setup();
    childPanel2Setup();
    childPanel3Setup();
    childPanel4Setup();
    childPanel5Setup();
    childPanel6Setup();
    childPanel7Setup();
    childPanel8Setup();
    childPanel9Setup();

    basePanel.add(childPanel1);
    basePanel.add(childPanel2);
    basePanel.add(childPanel3);
    basePanel.add(childPanel4);
    basePanel.add(childPanel5);
    basePanel.add(childPanel6);
    basePanel.add(childPanel7);
    basePanel.add(childPanel8);
    basePanel.add(childPanel9);

    Container cp = getContentPane();
    cp.setLayout(new BorderLayout());
    cp.add(basePanel, BorderLayout.CENTER);
}

public void childPanel1Setup()
{
    childPanel1 = new JPanel();
    childPanel1.setLayout(new GridLayout(row, column, 0, 0));
    childPanel1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares1 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares1[rows][cols] = new JButton();
            squares1[rows][cols].setSize(32, 32);
            squares1[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares1[rows][cols].setBackground(Color.WHITE);
            childPanel1.add(squares1[rows][cols]);
        }
    }
}

public void childPanel2Setup()
{
    childPanel2 = new JPanel();
    childPanel2.setLayout(new GridLayout(row, column, 0, 0));
    childPanel2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares2 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares2[rows][cols] = new JButton();
            squares2[rows][cols].setSize(32, 32);
            squares2[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares2[rows][cols].setBackground(Color.WHITE);
            childPanel2.add(squares2[rows][cols]);
        }
    }
}

public void childPanel3Setup()
{
    childPanel3 = new JPanel();
    childPanel3.setLayout(new GridLayout(row, column, 0, 0));
    childPanel3.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares3 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares3[rows][cols] = new JButton();
            squares3[rows][cols].setSize(32, 32);
            squares3[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares3[rows][cols].setBackground(Color.WHITE);
            childPanel3.add(squares3[rows][cols]);
        }
    }
}

public void childPanel4Setup()
{
    childPanel4 = new JPanel();
    childPanel4.setLayout(new GridLayout(row, column, 0, 0));
    childPanel4.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares4 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares4[rows][cols] = new JButton();
            squares4[rows][cols].setSize(32, 32);
            squares4[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares4[rows][cols].setBackground(Color.WHITE);
            childPanel4.add(squares4[rows][cols]);
        }
    }
}

public void childPanel5Setup()
{
    childPanel5 = new JPanel();
    childPanel5.setLayout(new GridLayout(row, column, 0, 0));
    childPanel5.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares5 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares5[rows][cols] = new JButton();
            squares5[rows][cols].setSize(32, 32);
            squares5[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares5[rows][cols].setBackground(Color.WHITE);
            childPanel5.add(squares5[rows][cols]);
        }
    }
}

public void childPanel6Setup()
{
    childPanel6 = new JPanel();
    childPanel6.setLayout(new GridLayout(row, column, 0, 0));
    childPanel6.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares6 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares6[rows][cols] = new JButton();
            squares6[rows][cols].setSize(32, 32);
            squares6[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares6[rows][cols].setBackground(Color.WHITE);
            childPanel6.add(squares6[rows][cols]);
        }
    }
}

public void childPanel7Setup()
{
    childPanel7 = new JPanel();
    childPanel7.setLayout(new GridLayout(row, column, 0, 0));
    childPanel7.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares7 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares7[rows][cols] = new JButton();
            squares7[rows][cols].setSize(32, 32);
            squares7[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares7[rows][cols].setBackground(Color.WHITE);
            childPanel7.add(squares7[rows][cols]);
        }
    }
}

public void childPanel8Setup()
{
    childPanel8 = new JPanel();
    childPanel8.setLayout(new GridLayout(row, column, 0, 0));
    childPanel8.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares8 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares8[rows][cols] = new JButton();
            squares8[rows][cols].setSize(32, 32);
            squares8[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares8[rows][cols].setBackground(Color.WHITE);
            childPanel8.add(squares8[rows][cols]);
        }
    }
}

public void childPanel9Setup()
{
    childPanel9 = new JPanel();
    childPanel9.setLayout(new GridLayout(row, column, 0, 0));
    childPanel9.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    squares9 = new JButton[row][column];

    for(int rows = 0; rows < this.row; rows++)
    {
        for(int cols = 0; cols < this.column; cols++)
        {
            squares9[rows][cols] = new JButton();
            squares9[rows][cols].setSize(32, 32);
            squares9[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            squares9[rows][cols].setBackground(Color.WHITE);
            childPanel9.add(squares9[rows][cols]);
        }
    }
}

}

2 个答案:

答案 0 :(得分:4)

我相信这是因为JPanel的布局管理器是FlowLayout,其中包含一个边距。创建面板时,将其布局设置为GridLayout,这将为您提供0余量。

您还为setVgap(0);完成了setHgap(0);GridLayout吗?

答案 1 :(得分:1)

GridLayout使所有组件的大小相同。如果需要在JPanel内部布置3x3组件的矩阵,它是100x100像素,每个组件将是33x33像素,并且在右侧和底部将有额外的像素,因为100不是因子3.如果JPanel是101x101像素,所有四个边缘都会有额外的像素。

考虑使用GridBagLayout或Box或其他比GridLayout更智能的布局。