我的程序中有3个类。Java布局问题
public class Field extends JLabel{
private int x, y;
public Field(int x, int y){
this.x = x;
this.y = y;
setOpaque(true);
setMinimumSize(new Dimension(50,50));
setPreferredSize(new Dimension(75,75));
if((x + y) % 2 == 0)
setBackground(Color.GREEN);
else
setBackground(Color.YELLOW);
}
public class Board extends JPanel{
public Field[][] fields = new Field[8][8];
public Board(){
setLayout(new GridLayout(8,8));
setMinimumSize(new Dimension(500,500));
setPreferredSize(new Dimension(550,550));
setBackground(Color.RED);
fillBoard();
}
private void fillBoard(){
for(int i = 0; i < 8; ++i){
for(int j = 0; j < 8; ++j){
fields[i][j] = new Field(i, j);
add(fields[i][j]);
}
}
}
public class GUI extends JFrame{
public Board board;
private GUI(){
board = new Board();
setLayout(new FlowLayout());
add(board);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
pack();
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new GUI();
}
});
}
}
每当我运行该程序,this出现,而不是一个黄绿色的电路板。任何人都可以帮忙吗?
你能更详细地描述你所期望它看起来像? – mgiuca
当我运行你的代码时,我得到一个绿色/黄色的棋盘,在右侧和底部都有一个红色的小边框。 –
大概只是一个错字,但'Field'类没有关闭 – barrowc