在网格布局JPanels差距 [英] Gap between JPanels in GridLayout

查看:386
本文介绍了在网格布局JPanels差距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下code生产中的9个按键9台单独的JPanels。九JPanels安排到使用网格布局基地的JPanel。然后,这个基地的JPanel放置到使用边界布局中的contentPane。我使用这些Jbutton边界和每一个人的JPanel明确界定它们的间距。每个JPanel的内这些Jbutton看起来不错,但有这是造成这是窃听了我的地狱的双重线的外观JPanels之间的差距。我试着将每一个人的JPanel到直接的contentPane但什么都不做。完全难住了,不知道如果任何人有什么建议吗?

The code below produces nine individual JPanels in which are 9 buttons. The nine JPanels are arranged onto a base JPanel using GridLayout. This base JPanel is then placed onto the ContentPane using Border Layout. I'm using borders for the JButtons and each individual JPanel to clearly define their separation. The JButtons inside each JPanel look fine but there is a gap between the JPanels which is causing the appearance of a double line which is bugging the hell out of me. I've tried adding each individual JPanel onto the ContentPane directly but this does nothing. Totally stumped and wondering if anyone has any suggestions?

P.S。我知道重复code需求质量在一个糟糕的方​​式重新分解和会,这只是实物模型的目的。

P.S. I know the mass of duplicate code needs re-factoring in a bad way and it will, this is just for mock-up purposes.

谢谢,

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

}

推荐答案

我相信这是因为布局管理器的JP​​anel s是的FlowLayout ,其中包括保证金。当您创建面板,设置其布局网格布局,这将给你0保证金。

I believe it's because the Layout Manager for JPanels is FlowLayout which includes a margin. When you create the panels, set their layout to GridLayout which will give you 0 margin.

另外你做了 setVgap(0); setHgap(0); 网格布局

这篇关于在网格布局JPanels差距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆