更改 JFrame 的可见性 class
Change JFrame's visibility from another class
我正在编写一个游戏,我在其中创建了一个暂停菜单 class PausePanel
。在那个 class 中,我希望能够设置不同的 class、MainPanel
的 JFrame
,但是每当我创建一个这样做的方法时,我都会得到不同的 errors/issues.
- Class
Game
是为了显示 PausePanel
初始化。
显示 - Class
PausePanel
是因为那是我需要代码帮助的地方。
Class 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
作为控件 class,监听 PausePanel
并调用 MainPanel
中的方法。
或者,您可以将对 MainPanel
实例的引用指向 PasuePanel
:
PausePanel pp = new PausePanel(mainPanel)
我正在编写一个游戏,我在其中创建了一个暂停菜单 class PausePanel
。在那个 class 中,我希望能够设置不同的 class、MainPanel
的 JFrame
,但是每当我创建一个这样做的方法时,我都会得到不同的 errors/issues.
- Class
Game
是为了显示PausePanel
初始化。
显示 - Class
PausePanel
是因为那是我需要代码帮助的地方。 Class
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
作为控件 class,监听 PausePanel
并调用 MainPanel
中的方法。
或者,您可以将对 MainPanel
实例的引用指向 PasuePanel
:
PausePanel pp = new PausePanel(mainPanel)