Java 更改JFrame';这是另一个类的可见性

Java 更改JFrame';这是另一个类的可见性,java,methods,jframe,jpanel,public,Java,Methods,Jframe,Jpanel,Public,我正在编写一个游戏,其中我创建了一个暂停菜单类PausePanel。在该类中,我希望能够设置不同类的JFrame,MainPanel,但每当我使用一种方法来设置时,就会出现不同的错误/问题 ClassGame用于显示PausePanel初始化 显示ClassPausePanel,因为这是我需要代码帮助的地方 类MainPanel具有JFrame我要更改的可见性 public class Game extends JFrame{ //Contains the game's panel

我正在编写一个游戏,其中我创建了一个暂停菜单类
PausePanel
。在该类中,我希望能够设置不同类的
JFrame
MainPanel
,但每当我使用一种方法来设置时,就会出现不同的错误/问题

  • Class
    Game
    用于显示
    PausePanel
    初始化
  • 显示Class
    PausePanel
    ,因为这是我需要代码帮助的地方
  • 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 );
        }
    }
    
    公共类游戏扩展JFrame{//包含游戏面板
    公共静态void main(字符串[]args){
    游戏g1=新游戏();
    g1.玩();
    }
    公共游戏(){
    setVisible(真);
    //其他东西
    }
    //其他方法
    私有类密钥扩展了KeyAdapter{
    @凌驾
    按下公共无效键(按键事件e){
    if(例如getKeyCode()==KeyEvent.VK_ESCAPE){
    setVisible(假);
    MainPanel.pause();
    PausePanel pp=新的PausePanel();
    pp.背景(颜色:黑色);
    pp.setPreferredSize(新尺寸(宽度、高度));
    pp.setLayout(空);
    }}}
    公共类PausePanel扩展JFrame{///标题屏幕
    公共PausePanel(){
    //其他东西
    }
    私有类ButtonListener实现ActionListener{
    已执行的公共无效操作(操作事件e){
    如果(如getSource().equals(continu)){
    
    可见性(true);您可以使用
    Game
    作为一个控制类,它侦听
    PausePanel
    并调用
    MainPanel
    中的方法
    或者,您可以将对
    MainPanel
    实例的引用路径设置为
    PasuePanel

    PausePanel pp = new PausePanel(mainPanel)
    

    你不能只靠自己做
    visibility(true)
    。你必须为你的
    PausePanel
    实例提供
    MainPanel
    (我们称之为
    mp
    )的某个实例,这样你就可以做
    mp.visibility(true)
    。事实上,
    mp
    可能需要成为
    Game
    类的实例变量。