2013-09-21 8 views
0

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

제 1의 나쁜 접근'pnl.setLayout (null);''paint'를 호출 한 후에'validate'를 호출하려고 시도하고'paintComponent'를 오버 라이하는 컨테이너에 페인트하지 말고 종종 윈도우에서 페인트하지 말것 – nachokk

+0

' 그래픽스 그래픽 = jFrame.getGraphics();'! 이것은 스윙에서 사용자 정의 그림이 수행되는 방식이 아닙니다. 당신이 – MadProgrammer

답변

3

하지 마십시오, 사용 Graphics graphics = jFrame.getGraphics(); (또는 getGraphics 일반적으로)! 이것은 스윙에서 사용자 정의 그림이 수행되는 방식이 아닙니다. you'v가 그래픽 컨텍스트를 제거했다는 단순한 사실이 핵심적인 문제입니다.

모든 그림은 바람직 확장하는 모든 구성 요소에서 paintComponent를 재정 의하여, 페인팅 API의 컨텍스트 내에서 수행해야 JComponent (I, 개인적으로, JPanel를 선호)

은 사용자 정의 구성 요소를 작성하고 당신을 사용자 정의 수행 사용 그림. 프레임의 다른 구성 요소와 함께 레이아웃하십시오.

Swing에서 페인팅이 작동하는 방식에 대한 자세한 내용은 Performing Custom PaintingPainting in AWT and Swing을 설정하십시오.

MouseListener 정말 버튼에 사용할 적절한 리스너 아니라, 더 나은 선택은

은 자세한 내용은 How to write an Action ListenerHow to use buttons를 참조하십시오 계정 마우스 클릭과 키보드 이벤트에 소요 ActionListener를 ... 사용하는 것입니다. ..

관련 문제