当前位置: 首页 > 知识库问答 >
问题:

GridLayout中JPanels之间的间隙

施自怡
2023-03-14

下面的代码生成九个单独的JPanels,其中有9个按钮。九个JPanel使用GridLayout排列到一个基本JPanel上。然后使用边框布局将这个基本JPanel放置到ContentPane上。我对JButtons和每个JPanel使用边框来明确定义它们的分离。每个JPanel中的JButtons看起来都很好,但是JPanels之间有一个间隙,这导致了双行的出现,这让我非常恼火。我尝试将每个JPanel直接添加到ContentPane中,但没有任何效果。完全被难倒了,想知道是否有人有什么建议?

谢谢,

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

}

共有1个答案

广献
2023-03-14

我认为这是因为JPanels的布局管理器是flowLayout,其中包括一个边距。当您创建面板时,将其布局设置为GridLayout,这将给您0页边距。

您是否为GridLayout执行了setvgap(0);sethgap(0);

 类似资料:
  • 我偶然发现了这种并发症,花了4个多小时调试和谷歌搜索,但无济于事... 基本上我这里有1个JFrame,2个JPanel。我将JFrame设置为JPanel的第1个内容窗格,当我运行应用程序时,JFrame将显示,其中包含JPanel。 现在这个JPanel里面有1个JButton,当我点击它时,我希望它切换到另一个JPanel。正如您从代码中看到的,当我单击JButton(AddProduct)

  • 问题内容: 我想通过单击JPanels上的按钮在JPanels之间切换。 例如:我有一个带有JButton simknop的JPanel sim和一个带有JButton helpknop的JPanel帮助,我想通过单击按钮在这两个JPanel之间进行切换。当我单击JButton simknop时,应显示JPanel帮助;当我单击JButton帮助时,应显示JPanel sim。 您可以在下面找到不

  • 我有一个带有多个面板的JFrame类,我通过使用paintComponent(graphics g)方法绘制所有的图形,因此使用CardLayout没有任何用处。要切换帧,我只需点击一个按钮,比如回车,但问题是,当我切换面板时,paintCompenent中剩下的图形仍然存在。我还尝试使用panel.setvisible(false)使面板可见,这有点帮助,只是我要转换到的面板仍然不可见,即使在将

  • 我试图使用GridBagLayout将两个JPanel放置到一个JFrame上。第一个JPanel使用gridLayout创建35列和27行JButtons,宽度应该是JFrame的3/4。此面板需要填充其可用的垂直空间。第二个JPanel也使用gridLayout,应该填充主JFrame的最后1/4。 不幸的是,我的第一个JPanel(sPan)甚至不适合屏幕,并迫使整个第二个JPanel(cP

  • 我需要减少两部分之间的空间。我看了这个问题,但解决方案不允许我的自定义页眉视图,因为它结合了页脚和页眉的空间。 这里是的图片。黑色是背景色。

  • 问题内容: 我有一张桌子,它是: 当我提出错误的要求时,例如要为唯一列输入相同的值。“ id”正在增加…这是错误的id请求; 这是我的表结果; 请帮助我如何解决此问题,我想订购。顺序的.. 问题答案: 这就是序列的工作方式。 重要提示:为避免阻塞从同一序列中获取数字的并发事务,绝不会回退nextval操作。也就是说,一旦获取了值,就将其视为已使用且不会再次返回。即使周围的事务稍后中止,或者调用查询