简体   繁体   English

添加的JPanel不显示Java

[英]Added JPanel does not show up Java

I've been trying to figure why the gameControlArt(which is a JPanel object) does not appear on the gamePanel which is also a JPanel object. 我一直试图弄清楚为什么gameControlArt(这是一个JPanel对象)没有出现在也是一个JPanel对象的gamePanel上。

What I am trying to create: the user sees the mainMenuArt(A JPanel object that has a JLabel containing artwork) adding on to the gamePanel upon running the program. 我要创建的内容:用户在运行程序时看到mainMenuArt(一个JPanel对象,其中包含一个包含图形的JLabel)添加到gamePanel上。 When user presses a key, mainMenuArt gets removed. 当用户按下一个键时,mainMenuArt被删除。 gameControlArt shows up. gameControlArt出现。 User presses a key again, gameControlArt gets removed, canvas object gets added to the gamePanel object. 用户再次按下键,将删除gameControlArt,将画布对象添加到gamePanel对象。

What winds up happening instead: the mainMenuArt appears on the gamePanel which is good. 相反发生的事情是:mainMenuArt出现在gamePanel上,很好。 The problem lies after removing the mainMenuArt, the gameControlArt(which is another JPanel object that has a JLabel object containing artwork) does not get added to the gamePanel(which is a JPanel object). 问题出在移除mainMenuArt之后,gameControlArt(这是另一个具有包含美术作品的JLabel对象的JPanel对象)没有被添加到gamePanel(这是JPanel对象)中。 Instead it shows the default gray area when a JPanel object has no components added to it. 相反,当JPanel对象没有添加任何组件时,它将显示默认的灰色区域。 When the user presses a key, the canvas shows up on the gamePanel. 当用户按下一个键时,画布将显示在gamePanel上。

So to sum what the problem is: the gameControlArt does not show up at all like it is suppose to on the gamePanel. 总结一下问题是什么:gameControlArt根本不像在gamePanel上那样显示。

I try updateUI(), revalidate and repaint() but those method calls do not work for some odd reason. 我尝试updateUI(),revalidate和repaint(),但由于某些奇怪的原因,这些方法调用不起作用。

public class GameWindow extends JFrame implements KeyListener{
gamePanel = (JPanel) getContentPane();

gamePanel.remove(mainMenuArt);
gameControlArt = new GameControlArt();
gamePanel.add(gameControlArt);
GameWindow.getGamePanel().revalidate();
}

public class extends Canvas implements KeyListener
{
GameWindow.getGamePanel().add(this);
GameWindow.getGamePanel().revalidate();
}

try getContentPane.add() instead of add. 尝试使用getContentPane.add()而不是add。

I'm not sure if it works in this case or not, since I don't know how you have defined gamePanel class, but I hope it does. 我不确定它是否在这种情况下有效,因为我不知道您如何定义gamePanel类,但我希望它能起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM