0

我正在用java制作跳棋,当我点击GUI时“新游戏”按钮消失了。当我将鼠标悬停在它上面时它会重新出现,但如果我单击 GUI,它会再次消失。你知道我做错了什么/做错了吗?

public void setFrame()
    {
        boardSize  = 10;
        squareSize = 50;
        int imageSize = boardSize * squareSize;       
        image = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_ARGB);
        imageIcon = new ImageIcon(image);
        jLabel = new JLabel(imageIcon);
        button = new JButton("New Game");
        button.setFocusable(false);
        button.setBounds(375, 5, 100, 20);
        pnl = new JPanel();
        pnl.setBounds(400, 10, 200, 100);
        pnl.setLayout(null);
        pnl.add(button);

        jFrame = new JFrame("Checker Board");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.add(jLabel, BorderLayout.CENTER);
        jFrame.add(pnl);
        jFrame.setSize(506, 558);
        jFrame.setResizable(false);
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);

        jFrame.validate();
    }

    /**
     * Paint the checker board onto the Image.
     */
    public void paint()
    {
        Graphics graphics = jFrame.getGraphics();

        pnl.paint(graphics);
        button.paint(graphics);

        graphics.setColor(Color.black);
        Font font = new Font("Score", Font.BOLD, 20);
        graphics.setFont(font);
        graphics.drawString("Score: ", 150, 47);
        graphics.drawString("Turn: ", 20, 47);
        graphics.setFont(font.deriveFont(0, 16.0F));
        graphics.drawString("Red: " + Game.score.getScoreRed() + "    Black: " + Game.score.getScoreBlack(), 230, 47);
        graphics.drawString((Game.redTurn ? "Red" : "Black"), 80, 47);

        // paint a red  board
        graphics.setColor(Color.red);
        graphics.fillRect(xShift, zShift, boardSize * squareSize, boardSize * squareSize);

        // paint the black squares
        graphics.setColor(Color.black);
        for (int row = 0; row < boardSize; row++)
        {
            for (int col = row % 2; col < boardSize; col += 2)
            {
                graphics.fillRect( row * squareSize + xShift, col * squareSize + zShift, squareSize, squareSize );
            }
        }

        for(int i = 0; i < 10; i++)
        {
            for(int j = 0; j < 10; j++)
            {
                if(Game.board.pieces[i][j] != null)
                {
                    Color pieceColor = Game.board.pieces[i][j].getColor().equals(EnumTeam.BLACK) ? Color.gray : Color.pink;
                    graphics.setColor(pieceColor);
                    graphics.fillOval((i * 50) + 10 + xShift, (j * 50) + 10 + zShift, 30, 30);
                    if(Game.board.pieces[i][j].isKing())
                    {
                        pieceColor = Game.board.pieces[i][j].getColor().equals(EnumTeam.BLACK) ? Color.darkGray : Color.magenta;
                        graphics.setColor(pieceColor);
                        graphics.fillOval((i * 50) + 20 + xShift, (j * 50) + 20 + zShift, 10, 10);
                    }
                }
            }
        }

        graphics.setColor(Color.cyan);
        drawRect(graphics, Game.board.getSelectedX(), Game.board.getSelectedZ(), 5);
    }
4

1 回答 1

3

永远不要使用Graphics graphics = jFrame.getGraphics();(或getGraphics一般)!这不是在 Swing 中完成自定义绘制的方式。您随后清除了图形上下文这一事实就是您的核心问题。

所有绘画都应该在绘画 API 的上下文中完成,最好通过覆盖paintComponent任何扩展的组件JComponent(我个人更喜欢JPanel

创建一个自定义组件并使用它执行自定义绘画。将其与框架上的其他组件一起布置。

设置在 AWT 和 Swing中执行自定义绘画和 绘画以了解有关绘画如何在 Swing 中工作的更多详细信息。

AMouseListener并不是真正适合用于按钮的侦听器,更好的选择是使用ActionListener考虑到鼠标点击和键盘事件的 a ...

有关详细信息,请参阅如何编写动作侦听器如何使用按钮...

于 2013-09-21T23:02:48.290 回答