簡體   English   中英

單擊GUI時JButton消失

[英]JButton disappears when GUI is clicked

我正在用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);
    }

永遠不要使用Graphics graphics = jFrame.getGraphics(); (或通常的getGraphics )! 這不是在Swing中完成自定義繪畫的方式。 然后清除圖形上下文的事實是您的核心問題。

所有繪畫都應在繪畫API的上下文中完成,最好是從擴展JComponent任何組件中重寫paintComponent (我個人更喜歡JPanel

創建一個自定義組件並使用它執行自定義繪制。 將其與框架上的其他組件一起布置。

設置“ 在AWT和Swing中 執行自定義繪畫繪畫”,以獲取有關繪畫如何在Swing中工作的更多詳細信息。

MouseListener並不是真正適合用於按鈕的偵聽器,更好的選擇是使用考慮到鼠標單擊和鍵盤事件的ActionListener

有關更多詳細信息,請參見如何編寫動作偵聽器如何使用按鈕

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM