2013-09-21 83 views
0

我在java中製作跳棋,點擊GUI時「新遊戲」按鈕消失。當我將鼠標懸停在它上面時,它會再次出現,但如果單擊GUI,它會再次消失。你知道我做錯了什麼嗎?我做錯了嗎?單擊GUI時JButton消失

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

如果您只發布代碼的相關部分,比如如何創建按鈕,面板/按鈕等上的點擊事件,我們將更容易幫助您。 – gwin003

+0

第一種不好的方法'pnl.setLayout(null);'在調用'paint'後嘗試調用'validate',並且經常說不要在窗口中繪製,而是在容器中繪製覆蓋'paintComponent' – nachokk

+0

不要使用' Graphics graphics = jFrame.getGraphics();'!這不是自定義繪畫在Swing中完成的方式。你的事實 – MadProgrammer

回答

3

不要,永遠使用Graphics graphics = jFrame.getGraphics();(或getGraphics一般)!這不是自定義繪畫在Swing中完成的方式。事實證明,你清除了圖形上下文是你的核心問題。

所有的畫應該畫API的範圍內進行,最好是從延伸到任何組件覆蓋paintComponentJComponent(我個人還是喜歡JPanel

創建自定義組件,並使用它,它執行您的自定義繪畫。使用框架上的其他組件進行佈局。

設置Performing Custom PaintingPainting in AWT and Swing瞭解更多關於繪畫在Swing中的工作原理。

一個MouseListener是不是真的合適的監聽器來使用按鈕,更好的選擇是使用一個ActionListener其中考慮到鼠標點擊和鍵盤事件...

詳情請參閱How to write an Action ListenerHow to use buttons。 ..