Java 在JPanel上绘制的网格不完全可见

Java 在JPanel上绘制的网格不完全可见,java,swing,Java,Swing,我有一个面板,它扩展了JPanel并覆盖了方法paintComponent(Graphics g),该方法绘制了一些正方形 public class Painter extends JPanel{ private static final Color orange = new Color(225, 95, 0); private Block[][] blocks; private int sizeBlock; public Painter() {

我有一个面板,它扩展了
JPanel
并覆盖了方法
paintComponent(Graphics g)
,该方法绘制了一些正方形

public class Painter extends JPanel{

    private static final Color orange = new Color(225, 95, 0);

    private Block[][] blocks;
    private int sizeBlock;

    public Painter() {
        this.setBackground(Color.WHITE);
    }

    public void setBlocks(Block[][] blocks) {   
        this.blocks = blocks;   
    }

    public void setSizeBlock(int size) {    
        this.sizeBlock = size;  
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        for(Block[] cols : blocks) {
            for(Block block : cols) {

                switch(block.getType()) {

                    case Block.wall: {

                        paintBlockLineBorder(g, block.getX()*(sizeBlock-1),
                                                block.getY()*(sizeBlock-1), orange);
                        break;

                    }

                    default: {break;}
                }           
            }       
        }       
    }

    private void paintBlockLineBorder(Graphics g, int x, int y, Color color) {

        //background
        g.setColor(color);
        g.fillRect(x, y, sizeBlock, sizeBlock);

        //border
        g.setColor(Color.BLACK);
        g.drawRect(x, y, sizeBlock-1, sizeBlock-1);

    }
}
我将这个
JPanel
(Painter)添加到另一个
JPanel
(painterPanel)中,这个
JPanel
(painterPanel)通过布局
GridBagLayout
添加到一个
JFrame

结果不是我想要的:

这就是我想要的:

下面是一个重现问题的小用例:

public static void main(String[] args) {

    JFrame mainFrame = new JFrame();
    mainFrame.setLayout(new GridBagLayout());
    mainFrame.setBounds(100, 100, 500, 400);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.fill = GridBagConstraints.NONE;
    gbc.gridx = 0; gbc.gridy = 0;
    gbc.weighty = 1; gbc.weightx = 1;

    JPanel painterPanel = new JPanel();
    painterPanel.setBorder(BorderFactory.createLineBorder(Color.black));

    mainFrame.add(painterPanel, gbc);

    Painter apainter = new Painter();

    painterPanel.add(apainter);

    Block[][] ablock = new Block[2][2];
    ablock[0][0] = new Block(0, 0, Block.wall);
    ablock[0][1] = new Block(0, 1, Block.wall);
    ablock[1][0] = new Block(1, 0, Block.wall);
    ablock[1][1] = new Block(1, 1, Block.wall);

    apainter.setBlocks(ablock);
    apainter.setSizeBlock(25);

    apainter.repaint();

    mainFrame.setVisible(true);     
}
下面是
类:

public class Block {

    public static final String wall = "wall";

    private int x;
    private int y;
    private String type;

    public Block(int x, int y, String type) {

        this.x = x;
        this.y = y;
        this.type = type;
    }

    //getters & setters
}
为什么我的
画师不完全可见?

请尝试以下操作:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main{

    public static void main(String[] args) throws InterruptedException{

            JFrame mainFrame = new JFrame();

            Block[][] ablock = new Block[2][2];
            ablock[0][0] = new Block(0, 0, Block.wall);
            ablock[0][1] = new Block(0, 1, Block.wall);
            ablock[1][0] = new Block(1, 0, Block.wall);
            ablock[1][1] = new Block(1, 1, Block.wall);

            Painter apainter = new Painter();
            mainFrame.add(apainter);
            apainter.setBlocks(ablock);
            apainter.setSizeBlock(25);

            mainFrame.pack();
            mainFrame.setVisible(true);
    }
}

class Painter extends JPanel{

    private static final Color orange = new Color(225, 95, 0);

    private Block[][] blocks;
    private int sizeBlock;

    public Painter() {

       setBackground(Color.WHITE);
       setBorder(BorderFactory.createLineBorder(Color.black));
       setPreferredSize(new Dimension(400, 300));
    }

    public void setBlocks(Block[][] blocks) {

        this.blocks = blocks;
    }

    public void setSizeBlock(int size) {
        sizeBlock = size;
        //you may want to update Preferred Size
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        for(Block[] cols : blocks) {
            for(Block block : cols) {

                switch(block.getType()) {

                    case Block.wall: {

                        paintBlockLineBorder(g, block.getX()*(sizeBlock-1),
                                                block.getY()*(sizeBlock-1), orange);
                        break;
                    }

                    default: {break;}

                }
            }
        }
    }

    private void paintBlockLineBorder(Graphics g, int x, int y, Color color) {

        //background
        g.setColor(color);
        g.fillRect(x, y, sizeBlock, sizeBlock);

        //border
        g.setColor(Color.BLACK);
        g.drawRect(x, y, sizeBlock-1, sizeBlock-1);

    }
}

class Block {

    public static final String wall = "wall";

    private final int x;
    private final int y;
    private final String type;

    public Block(int x, int y, String type) {

        this.x = x;
        this.y = y;
        this.type = type;
    }

    int getX() {return x;   }
    int getY() {return y;   }
    String getType() {  return type;}
}
如果需要,请随时要求澄清。

也可以考虑使用<代码> GridLayout < /COD>:

< P>在@ CalOxHubgGER的建议中,我覆盖了<代码> GETAPYREST Stabor <代码>,
getMinimumSize
getMaximumSize
返回一个
维度
,其值基于
块的长度
sizeBlock的值

@CarlosHeuberger,而该代码中还有其他东西需要调整,是的,这就是问题的症结所在。@CarlosHeuberger我覆盖了
getPreferredSize
getMinimumSize
getMaximumSize
,现在它可以工作了。您能否创建一个简短的答案来选择答案@AndrewThompson应该优化什么?太多了。首先:一个循环,用于生成所有块的数组,
pack()
要调整帧的大小,
setVisible(…)
之前的
repaint()
是多余的,将被忽略。。好的,这可能是它的主要部分@AndrewThompson请,让我们在这里谈谈:“让我们在这里谈谈:”谢谢,不,我不这么‘聊天’。