2016-12-08 1 views
1

이 JPanel에서 JButton "BackToTheMenu"는 패널 상단에 있으며 클릭 할 수있는 곳을 알고 있지만 볼 수는 없습니다. 그것을 클릭하면 다음 패널로 완벽하게 이동합니다. 어떻게 나타나야합니까!? 도움말 정말 고맙습니다!내 JButton이 전혀 표시되지 않지만 계속 작동하는 이유는 무엇입니까? (내가 그것을 클릭 할 수있는 곳을 알고 있습니다.)

public class GridPanel extends JPanel implements ActionListener 
{ 
    Ball ball = new Ball(); 
    Timer timer = new Timer(14, this); 

    JButton backToTheMenu = new JButton("To the Menu"); 

    public GridPanel() 
    { 
     setBackground(Color.WHITE); 
     setPreferredSize(new Dimension(900, 710)); 
     add(ball); 

     backToTheMenu.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent arg0) 
      {  
       ProjectileGame.gridButtonPressed(); 
      } 
     }); 
    } 

    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 

     g.setColor(Color.RED); 
     g.fillRect(0, 649, 30, 33); 

     g.setColor(Color.BLUE); 

     for (int i = 0; i < 35; i++) 
     { 
      g.setColor(Color.GREEN); 
      g.drawLine(0, (20+(30*i)), 900, (20+(30*i))); 
      g.drawLine((30+(30*i)), 0, (30+(30*i)), 1000); 
     } 

     ball.paintComponent(g); 

     g.setColor(Color.RED); 
     g.drawLine(0, 650, 900, 650); 
     g.drawLine(30, 0, 30, 1000); 

     Graphics2D g2d1 = (Graphics2D)g; 

     g.setColor(Color.BLACK); 
     g2d1.drawString("X Displacement (metres)", 400, 667); 
     AffineTransform at = new AffineTransform(); 
     at.setToRotation(Math.PI/-1.97); 
     g2d1.setTransform(at); 
     g2d1.drawString("Y Displacement (metres)", -380, 8);  

     setOpaque(false); 

     for (Component child : getComponents()) 
     { 
      child.repaint(); 
     } 

    }     

    public void actionPerformed(ActionEvent e) 
    { 
     ball.ballPhysics(); 
     repaint(); 
     timer.restart(); 
    } 
} 
+0

1), 게시 A [MCVE] 또는 [짧은, 콘도, 올바른 예 (http://www.sscce.org /). 2)'backToTheMenu'는 절대로 패널에 추가되지 않습니다. –

+0

'public void paintComponent (Graphics g) { super.paintComponent (g); ..이 스 니펫의 마지막 줄은 달성한다 .. for (Component child : getComponents()) { child.repaint(); }'..이 스 니펫. –

+0

'ProjectileGame.gridButtonPressed()'가 다른 곳에서 호출되지 않았습니까? 또한'actionPerformed()'에 몇몇 디버그 라인을 추가 할 수 있습니까? 단지 그 동작이 버튼에서 수행되는지 확인하기 위해서입니까? – Arvind

답변

0

난 당신이 코드에서이 곳이 있는지 확실하지 않습니다,하지만 난 당신이 backToTheMenu를 추가 표시되지 않습니다. 이 코드의 경우 :

add(backToTheMenu); 

을하지만 당신은 다른 곳을 추가하는 경우이를 무시 :

public GridPanel() 
{ 
    setBackground(Color.WHITE); 
    setPreferredSize(new Dimension(900, 710)); 
    add(ball); 

    backToTheMenu.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent arg0) 
     {  
      ProjectileGame.gridButtonPressed(); 
     } 
    }); 
} 

이 필요합니다.

+0

오, 그래, 내 질문에 코드를 넣는 것을 잊어 버렸지 만 지금은 다시 넣었지만 아직 나타나지 않습니다. 고마워요. –

2

많은 코드가 잘못되었습니다.

도장 방법은 그림 전용입니다. 다음과 같아야합니다.

  1. Invoke ball.paintComponent(). 패널은 추가 된 모든 구성 요소를 자동으로 그립니다.

  2. 하위 구성 요소를 가져 와서 repaint()를 호출하십시오. 다시 말하지만, 패널은 모든 하위 구성 요소를 칠합니다.

  3. 호출 setOpaque(...). 불투명 한 속성은 클래스의 생성자에서 설정해야합니다.

"메뉴로 돌아 가기"버튼은이 클래스에 정의되어서는 안됩니다. 상위 클래스에서 정의해야합니다.

그래서 코드가 같은 것을 보일 것입니다 : 더 나은 도움을 빨리 들어

JButton back = new JButton("Back to the Menu"); 
back.addActionListener(...); 
frame.add(back, BorderLayout.PAGE_START); 
JPanel grid = new GridPanel(); 
frame.add(grid, BorderLayout.CENTER); 
+0

정말 고마워요. 부모 클래스의 일부 코드를 보여 주면 처음으로이 작업을 수행 한 이후로 위의 "계획"을 구현하는 방법을 보여줄 수 있습니까? 어떻게 보여줄까요? 나는 현재 두 개의 JPanel, 즉 menuPanel과 gridPanel과 함께 cardLayout을 사용하고 있으며,이 버튼은 menuPanel로 돌아 간다. 이 JPanel은 JFrame에 추가되는 contentPanel에 추가됩니다. 그러면 어떻게 구현할 것인가? 고맙습니다 –

관련 문제