简体   繁体   English

单击GUI时JButton消失

[英]JButton disappears when GUI is clicked

I'm making checkers in java and the "New Game" button disappears when I click on the GUI. 我正在用Java进行检查,当我单击GUI时,“新游戏”按钮消失了。 It reappears when I hover my mouse over it, but will disappear again if I click the GUI. 当我将鼠标悬停在它上面时,它会重新出现,但是如果单击GUI,它将再次消失。 Do you know what I did wrong/am doing incorrectly? 你知道我做错了/我做错了吗?

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

Do not, ever use Graphics graphics = jFrame.getGraphics(); 永远不要使用Graphics graphics = jFrame.getGraphics(); (or getGraphics generally)! (或通常的getGraphics )! This is not how custom painting is done in Swing. 这不是在Swing中完成自定义绘画的方式。 The mere fact that you'v then cleared the graphics context is your core problem. 然后清除图形上下文的事实是您的核心问题。

All painting should be done within the context of the painting API, preferably by overriding paintComponent from any component that extends JComponent (I, personally, prefer JPanel ) 所有绘画都应在绘画API的上下文中完成,最好是从扩展JComponent任何组件中重写paintComponent (我个人更喜欢JPanel

Create a custom component and use it it perform you custom painting. 创建一个自定义组件并使用它执行自定义绘制。 Layout it out with your other components on the frame. 将其与框架上的其他组件一起布置。

Set Performing Custom Painting and Painting in AWT and Swing for more details about how painting works in Swing. 设置“ 在AWT和Swing中 执行自定义绘画绘画”,以获取有关绘画如何在Swing中工作的更多详细信息。

A MouseListener is not really an appropriate listener to use for buttons, a better choice would be to use a ActionListener which takes into account mouse clicks and keyboard events... MouseListener并不是真正适合用于按钮的侦听器,更好的选择是使用考虑到鼠标单击和键盘事件的ActionListener

See How to write an Action Listener and How to use buttons for more details... 有关更多详细信息,请参见如何编写动作侦听器如何使用按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM