我尝试在java中制作棋盘游戏我在棋盘上有问题我必须最大化然后最小化窗口以获得想要的棋盘我制作一个方形的颜色和位置和另一个用于棋盘
package eg.edu.guc.loa.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BoardGUI extends JFrame
{
public BoardGUI()
{
setSize(600, 600);
setVisible(true);
getContentPane().setLayout(new GridLayout(8,8));
JPanel [][]board= new JPanel[8][8];
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if(i%2==0)
{
if(j%2==0)
{
board[i][j]=new square(Color.black,0);
add(board[i][j],BorderLayout.CENTER);
}
else
{
if(j%2!=0)
{
board[i][j]=new square(Color.white,0);
add(board[i][j],BorderLayout.CENTER);
}
}
}
else
{
if(i%2!=0)
{
if(j%2==0)
{
board[i][j]=new square(Color.white,0);
add(board[i][j]);
}
else
{
if(j%2!=0)
{
board[i][j]=new square(Color.black,0);
add(board[i][j]);
}
}
}
}
}
}
setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH)
}
}
这是方形类
package eg.edu.guc.loa.gui;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class square extends JPanel
{
private Color c;
private int x;
public square(Color c,int x)
{
this.x=x;
this.c=c;
}
public void paint(Graphics g)
{
super.paint(g);
g.drawRect(x,x, 80,80);
g.setColor(c);
g.fillRect(x,x,80,80);
}
}