java - 如何将不同类的图形组件添加到 jframe 中?

标签 java swing graphics jframe

我正在制作一个棋盘 gui JFrame,它在 JFrame 的北侧有一个菜单栏,在 JFrame 的中间有一个实际的棋盘,在应用程序的底部有一个状态 JPanel。不过,我在 JFrame 上显示棋盘时遇到问题。有一个棋盘类(代表一个正方形),一个棋盘类(将所有棋盘放在一起),以及一个将棋盘放在 GUI 上的棋盘游戏类。我现在只需要在 JFrame 上显示棋盘的帮助,而不需要功能方面的帮助。我的 Checkergame 类构造函数:

    frame.setSize(505, 585);
    frame.setLayout(new BorderLayout());


    JMenuBar menuBar= new JMenuBar();
    JMenu menuTwo = new JMenu("Game");
    JMenuItem newGame = new JMenuItem("New Game");
    JMenuItem exit = new JMenuItem("Exit");
    menuTwo.add(newGame);
    menuTwo.add(exit);
    menuBar.add(menuTwo);


    JMenu helpTwo = new JMenu("Help");
    JMenuItem rules = new JMenuItem("Checker Game Rules");
    JMenuItem about = new JMenuItem("About Checker Game App");
    helpTwo.add(rules);
    helpTwo.add(about);
    menuBar.add(helpTwo);
    frame.add(menuBar , BorderLayout.NORTH);

    JPanel game = new JPanel();
    CheckerBoard c = null;


    try {
        frame.getContentPane().add(new CheckerBoard(boardStatus));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    frame.add(game, BorderLayout.CENTER);

    JPanel status = new JPanel();
    status.setLayout(new GridLayout());
    JLabel statusTwo = new JLabel("12 Red : 12 Black");

    status.add(statusTwo);
    status.add(name);
    frame.add(statusTwo, BorderLayout.SOUTH);



    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

我的棋盘类:

public class CheckerBoard extends JPanel {
private char[][] boardStatus;

public CheckerBoard(char [][] boardStatus) throws Exception {
    JPanel board = new JPanel();

    board.setLayout(new GridLayout(8,8));


    for (int row = 0; row < 8; row++) {
        for (int column = 0; column < 8; column++) {
            board.add(new CheckerPiece(row, column, boardStatus[row][column]));
        }
    }

    board.setVisible(true);



}

我的棋盘类:

public class CheckerPiece extends JComponent{

    private int row;
    private int column;
    private char status;
    final int y1 = 60;
    final int x1 = 480;


    public CheckerPiece(int row, int column, char status) throws Exception {

        setRow(row);
        setColumn(column);
        setStatus(status);
    }

    public int getRow() {
        return row;
    }



    public void setRow(int row) throws Exception {
        if (row >= 0 && row <= 7) {
            this.row = row;
        } else throw new IllegalCheckerboardArgumentException("Row must be between 0 and 7");

    }



    public int getColumn() {
        return column;
    }



    public void setColumn(int column) throws Exception {
        if (column >= 0 && column <= 7) {
            this.column = column;
        } else throw new IllegalCheckerboardArgumentException("Column must be between 0 and 7");

    }



    public char getStatus() {
        return status;
    }



    public void setStatus(char status) throws Exception {
        if (status == 'b' || status == 'e' || status == 'r')
        this.status = status;
        else throw new IllegalCheckerboardArgumentException("Status must be 'b', 'e', or 'r'.");
    }

    public void paintComponents(Graphics g) {
        int x = 0;
        int y = 0;
        int count = 0;
        if (status == 'b' ) {
            g.setColor(Color.GREEN);
            g.fillRect(0 + x, 0 + y, 60, 60);
            g.setColor(Color.BLACK);
            g.fillOval(5+x, 5 + y, 40, 40);
            x += 60;
            count++;
        }
        if (status == 'e') {
        g.setColor(Color.WHITE);
        g.fillRect(0 + x,0 + y, 60, 60);
        x+= 60;
        count++;
        }
        if (status == 'r') {
            g.setColor(Color.GREEN);
            g.fillRect(0 + x,0 + y, 60, 60);
            g.setColor(Color.RED);
            g.fillOval(5 + x, 5 + y, 40, 40);
            x += 60;
            count++;
        }
        if (count % 8 == 0) {
            y += 60;
        }


    }


}

最佳答案

您需要在 CheckerBoard 类中使用一个方法来返回 JPanel,以便将其添加到 JFrame 中。

import java.awt.GridLayout;

import javax.swing.JPanel;

public class CheckerBoard extends JPanel {
    private static final long serialVersionUID = 1L;

    private JPanel board;

    public CheckerBoard(char [][] boardStatus) 
            throws Exception {
        int height = boardStatus.length;
        int width = boardStatus[0].length;
        board = new JPanel();
        board.setLayout(new GridLayout(width, height));

        for (int row = 0; row < width; row++) {
            for (int column = 0; column < height; column++) {
                board.add(new CheckerPiece(row, column, 
                        boardStatus[row][column]));
            }
        }
    }

    public JPanel getPanel() {
        return board;
    }

}

关于java - 如何将不同类的图形组件添加到 jframe 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61464886/

相关文章:

java - 关闭 Swing 应用程序时进行整理

java - Swing 定时器的浮点值?

java - 将图像调整为任何给定的四边形

java - ListIterator 和并发修改异常的问题

java - 在java中以矩阵形式操作彩色图像

java - 将 HEIF 图像转换为 Java 中更常见的图像格式

java - 在java中突出显示单词

c - C图形:如何在屏幕上绘制x-y轴?

java - 从 fragment 创建一个通用方法来设置结果

java - 复杂继承/包组合中的包私有(private)成员可见性 - Qiuck 验证可见性的一般规则