2017-05-13 21 views
1

나는 일시 중지 메뉴 클래스 PausePanel을 만든 게임을 코딩하고 있습니다. 이 클래스에서는 MainPanel이라는 다른 클래스의 JFrame을 설정할 수 있기를 원하지만, 그렇게 할 때마다 다른 오류/문제가 발생합니다.다른 클래스에서 JFrame의 가시성을 변경하십시오.

  1. GamePausePanel 초기화를 표시합니다.
  2. 클래스 PausePanel이 표시되었으므로 코드에 도움이 필요합니다.
  3. 클래스 MainPanelJFrame의 가시성을 변경하고 싶습니다.

    public class Game extends JFrame{ //Contains the game's panel 
        public static void main(String[] args){ 
        Game g1 = new Game(); 
        g1.play(); 
    } 
    public Game(){ 
        setVisible(true); 
        //other stuff 
    } 
    //other methods 
    private class Keys extends KeyAdapter { 
        @Override 
        public void keyPressed(KeyEvent e){ 
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE){ 
         setVisible(false); 
         MainPanel.pause(); 
         PausePanel pp = new PausePanel(); 
         pp.setBackground(Color.BLACK); 
         pp.setPreferredSize(new Dimension(WIDTH,HEIGHT)); 
         pp.setLayout(null); 
    }}} 
    
    
    
    public class PausePanel extends JFrame{ //Title Screen 
        public PausePanel(){ 
         //other stuff 
        } 
        private class ButtonListener implements ActionListener{ 
         public void actionPerformed(ActionEvent e) { 
          if (e.getSource().equals(continu)){ 
           visibility(true); <------The issue 
          } 
    }}} 
    
    
    
    public class MainPanel extends JPanel{ //Actual game 
        private JPanel jp = new JPanel(); 
        public MainPanel(){ 
         //stuff 
        } 
        public void visibility(boolean b){ <----This is the method I'd like to be able to use 
         jp.setVisible(b); 
        } 
    } 
    
+0

당신은 그냥 할 수없는 '그 자체 가시성'(참). 'PausePanel' 인스턴스에'MainPanel' 인스턴스 ('mp'라고 부릅니다)를 제공해야만 mp.visibility (true)를 할 수 있습니다. 상황에 따라,'mp'는 아마도'Game' 클래스의 인스턴스 변수 일 필요가있을 것입니다. –

답변

2

당신은 PausePanel에 귀를 기울이며 MainPanel의 메소드를 호출하는 컨트롤 클래스로 Game를 사용할 수 있습니다.
은 또한 당신이 경로 PasuePanel-MainPanel 인스턴스에 대한 참조는 할 수

PausePanel pp = new PausePanel(mainPanel)