我正在编写一个游戏,在其中创建了一个暂停菜单类PausePanel
。在该类中,我希望能够设置不同类 MainPanel
的 JFrame
,但是每当我创建一个方法来执行此操作时,我都会遇到不同的错误/问题.
- 类
Game
用于显示PausePanel
初始化。 - 显示了
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 ); } }
最佳答案
您可以使用Game
作为控件类,它监听PausePanel
并调用MainPanel
中的方法。
或者,您可以将对 MainPanel
实例的引用传递给 PasuePanel
:
PausePanel pp = new PausePanel(mainPanel)
关于java - 从另一个类更改 JFrame 的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43948906/