开发者

Java layout problem

开发者 https://www.devze.com 2023-03-15 15:50 出处:网络
I\'ve got 3 classes in my program. public class Field extends JLabel{ private int x, y; public Field(int x, int y){

I've got 3 classes in my program.

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));
        set开发者_高级运维PreferredSize(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();
            }
        });
    }

}

Whenever I run the program, this appears instead of a yellow-green board. Can anyone help please?


Your posted code does not compile. Re-factoring into nested classes made it work correctly as shown, so I'm guessing you have a project level problem.

Java layout problem

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GUI extends JFrame {

    private 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() {

            @Override
            public void run() {
                new GUI();
            }
        });
    }

    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]);
                }
            }
        }
    }

    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);
            }
        }
    }
}


You should set layout manager and add component to the contentPane on JFrame, inside GUI you should call:

getContentPane().setLayout(new FlowLayout());
getContentPane().add(board);

Your code compiles if both Board and Field are refactored to inner classes, but the result looks like this

Java layout problem

To make the interface look without the red line comment setPreferredSize(new Dimension(550,550)); on your Board constructor so it become:

public Board(){
        int rows = 8,cols = 8;
    setLayout(new GridLayout(rows,cols));
    setMinimumSize(new Dimension(500,500));
    //setPreferredSize(new Dimension(550,550));
    setBackground(Color.RED);
    fillBoard();
}

Java layout problem

0

精彩评论

暂无评论...
验证码 换一张
取 消