我正在制作一个跳棋游戏,以帮助我学习Java的GUI。我正在使用JLayeredPane boardAndPieces来容纳2个JPanels:board和boardPiecesPanel。 board可以容纳另外两个JPanels:boardPanel和boardButtonPanel。 boardPanel是跳棋游戏板,据我所知,到目前为止没有任何问题。 boardButtonPanel拥有几个用于播放的按钮,但也不应该引起问题。以下是相关代码:

`

boardAndPieces = new JLayeredPane();//a layered pane to hold the pieces and board panels
board = new JPanel(new BorderLayout());//A panel to hold the board and buttons
boardPanel = new JPanel(new GridLayout(8, 8));//the checker board. Within the JPanel board
boardPiecesPanel = new JPanel(new GridLayout(8,8));//The panel that will hold the pieces
board.add(boardPanel, BorderLayout.NORTH);
boardButtonPanel = new JPanel(new GridLayout(1, 3));//holds the buttons used for play. Within the JPanel board
board.add(boardButtonPanel, BorderLayout.SOUTH);
//...add to the board in this space. This works fine

boardAndPieces.add(board, Integer.valueOf(1));
boardAndPieces.add(boardPiecesPanel, Integer.valueOf(2));

//Draw initial pieces
boardPiecesPanel.add(new RedPieceDrawer);


//Class RedPieceDrawer looks like this:
class RedPieceDrawer extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.red);
        g.drawOval(30, 30, 40, 40);

    }
}


`

使用此代码,将显示按钮和棋盘格,但不会出现圆圈或boardPiecesPanel中的任何内容。

最佳答案

有许多基本问题...


您的RedPieceDrawer没有任何大小调整提示,这些提示使布局管理器可以决定如何最好地布局此组件
默认情况下,JLayeredPane根本不使用布局管理器,这意味着当您向其中添加组件时,它们没有初始大小,这意味着不会渲染


使用当前的方法,首先将棋盘格放置在板上也会遇到问题。

更好的方法可能是简单地创建一系列用作单元的面板,然后直接将游戏片段add直接...

例如



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Checkers {

    public static void main(String[] args) {
        new Checkers();
    }

    private JPanel[] cells;

    public Checkers() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridLayout(8, 8));

                cells = new JPanel[8 * 8];
                int col = 0;
                int counter = 0;
                for (int index = 0; index < cells.length; index++) {

                    JPanel panel = new JPanel(new BorderLayout());
                    cells[index] = panel;
                    panel.setPreferredSize(new Dimension(40, 40));
                    panel.setMinimumSize(panel.getPreferredSize());
                    if (counter % 2 == 0) {
                        panel.setBackground(Color.BLACK);
                    } else {
                        panel.setBackground(Color.WHITE);
                    }

                    frame.add(panel);

                    counter++;
                    col++;
                    if (col > 7) {
                        counter++;
                        col = 0;
                    }

                }

                int row = 3;
                col = 4;
                int cell = (row * 8) + col;

                cells[cell].add(new GamePiece(Color.RED));

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class GamePiece extends JPanel {

        public GamePiece(Color color) {
            setOpaque(false);
            setBackground(color);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            int dim = Math.max(getWidth() - 4, getHeight() - 4);
            int x = (getWidth() - dim) / 2;
            int y = (getHeight() - dim) / 2;
            g2d.setColor(getBackground());
            g2d.fillOval(x, y, dim, dim);
            g2d.dispose();
        }

    }

}


现在,如果您不打算进行单元格转换,则可以改用二维数组。

我也很想在每个面板和/或游戏部件上使用单个MouseListener,以便您可以管理用户交互...

10-04 12:26