我的程序中有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