2017-05-13 11 views
1

私はポーズメニュークラスPausePanelを作成したゲームをコーディングしています。そのクラスでは、MainPanelという別のクラスのJFrameを設定できるようにしたいのですが、その方法を作成するときはいつも、別のエラーや問題が発生します。JFrameの可視性を別のクラスから変更する

  1. GamePausePanelの初期化が表示されます。
  2. クラスPausePanelが表示されています。なぜなら、私はコードの助けを必要としているからです。
  3. クラスMainPanelにはJFrameが表示されています。

    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) 
関連する問題