使用gridlayout将项目放入面板

时间:2017-12-25 06:52:24

标签: java graphics jframe grid-layout

嘿大家我试图用Java绘制Java棋盘格。我为游戏板的方块创建了一个方形类。

方形等级:

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

public class Square extends JPanel {

private int width = 80;
private int height = 80;
private int x,y;
private Color color;

public Square(int x, int y, Color color) {
    this.x = x;
    this.y = y;
    this.color = color;
}

public void paint(Graphics graphics){
    //setSize(new Dimension(width,height));
    graphics.setColor(color);
    graphics.drawRect(x,y, width,height);
    graphics.fillRect(x,y,width,height);
}
}

基本上我想创建一个网格布局为8乘8的面板。然后添加方形对象网格布局面板。我希望第一行包含红色,黑色,红色,黑色,红色,黑色,红色,黑色方块,第二行包含黑色,红色,黑色,红色,黑色,红色,黑色,红色方块。

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setPreferredSize(new Dimension(600,600));

    JPanel panel = new JPanel(new GridLayout(8,8));
    panel.setLayout(new GridLayout(8, 8));
    panel.setBackground(Color.green);

    Square redsqr1 = new Square(0,0, Color.RED);
    Square blksqr1 = new Square(0,0, Color.BLACK);
    Square redsqr2 = new Square(0,0, Color.RED);
    Square blksqr2 = new Square(0,0, Color.BLACK);
    Square redsqr3 = new Square(0,0, Color.RED);
    Square blksqr3 = new Square(0,0, Color.BLACK);
    Square redsqr4 = new Square(0,0, Color.RED);
    Square blksqr4 = new Square(0,0, Color.BLACK);

    Square redsqr5 = new Square(0,0, Color.RED);
    Square blksqr5 = new Square(0,0, Color.BLACK);
    Square redsqr6 = new Square(0,0, Color.RED);
    Square blksqr6 = new Square(0,0, Color.BLACK);
    Square redsqr7 = new Square(0,0, Color.RED);
    Square blksqr7 = new Square(0,0, Color.BLACK);
    Square redsqr8 = new Square(0,0, Color.RED);
    Square blksqr8 = new Square(0,0, Color.BLACK);

    panel.add(redsqr1);
    panel.add(blksqr1);
    panel.add(redsqr2);
    panel.add(blksqr2);
    panel.add(redsqr3);
    panel.add(blksqr3);
    panel.add(redsqr4);
    panel.add(blksqr4);
    panel.add(blksqr5);
    panel.add(redsqr5);
    panel.add(blksqr6);
    panel.add(redsqr6);
    panel.add(blksqr7);
    panel.add(redsqr7);
    panel.add(blksqr8);
    panel.add(redsqr8);

    frame.getContentPane().add(panel);


    frame.pack();
    frame.setVisible(true);

}

当我运行程序时,我得到输出PROGRAM OUTPUT HERE

只是好奇为什么输出放在2列中,每个方块之间有很大的空间。我怎样才能让他们并排排在一排,让第一排包含红色,黑色,红色,黑色,红色,黑色,红色,黑色方块,第二排包含黑色,红色,黑色,红色,黑色,红色,黑色,红色方块。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

这是因为你没有将所有需要的64个方块添加到布局中。因此布局单元将被拉伸以填充所有空间。结果输出会很混乱。此外,将水平和垂直间隙设置为0是个好主意。还有一个提示是通过调用JFrame#add它将组件添加到contentPane,并且不需要让contentPane向JFrame添加内容。此外,基础JPanelJFrame之间不需要中间人Square

此外,我稍微改变了main方法,以减少创建和添加Square s到输出框架的艰辛:

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.setPreferredSize(new Dimension(600,600));
    frame.setLayout(new GridLayout(8, 8, 0, 0));
    frame.getContentPane().setBackground(Color.green);

    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            frame.add(new Square(0, 0, (i+j)%2==0 ? Color.RED : Color.BLACK));
        }
    }   

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

希望这会有所帮助。