gpt4 book ai didi

java - 从 JPanel 中删除 JPanel - 仅适用于其自己的类

转载 作者:行者123 更新时间:2023-12-01 22:33:42 25 4
gpt4 key购买 nike

对此有很多问题,但他们都提出了重新验证的东西。我的代码在一个类中工作,但如果它从另一个类触发,则不会。

我有一个 JPanel,我想向其中添加一个 JPanel。

我有一个 startGame() 方法,应该删除当前添加到其中的面板。

如果我直接从构造函数调用 startGame() 方法,它就会起作用。

如果我从另一个类调用 startGame() 方法,该方法将不起作用。

public TopMenu topMenu;
public JPanel welcomeScreen;
public JPanel gameScreen;

public WelcomePanel() {
setLayout(null);
setSize(1000, 700);

init();

setBackground(Color.ORANGE);
}

public void init() {
topMenu = new TopMenu(this);
welcomeScreen = new WelcomeScreen();
gameScreen = new Game();

add(topMenu);
add(this.welcomeScreen);
}

public void startGame() {
remove(this.welcomeScreen);
add(gameScreen);
revalidate();
}

所以如果 - 在初始化之后 - 我会调用 startGame() 它会起作用,并且它会删除welcomeScreen并添加gameScreen。

如果我从 topMenu 中的 Action 监听器调用它,它不会执行任何操作。请勿移除旧面板,或在旧面板上添加新面板。

其他类:

欢迎屏幕

public class WelcomeScreen extends JPanel {
public WelcomeScreen()
{
setBounds(0, 50, GameBase.gameWidth, GameBase.gameHeight - 50);
setBackground(Color.GREEN);
}
}

游戏

public class Game extends JPanel {

public Image background;

public Game()
{

background = new ImageIcon("src/game/images/mainMenu/MainMenu.png").getImage();

setBounds(50, 50, 700, 650);
setBackground(Color.GREEN);
}

public void paintComponent(Graphics g) {
g.drawImage(background, 0, 0, null);
}
}

TopMenu(基本上: Action 监听器调用按钮上的 startGame 方法 - 已测试且有效。)

public class TopMenu extends JPanel implements ActionListener {

public topMenuButton playButton;
public topMenuButton forwardButton;
public topMenuButton menuButton;
public topMenuButton goToTheGameButton;

public Image background;

public static boolean playingNow = false;
public static boolean changingSettings = false;
public static boolean inTheMenu = true;

WelcomePanel welcomePanel;

public TopMenu(WelcomePanel welcomePanel) {
this.welcomePanel = welcomePanel;

background = new ImageIcon("src/game/images/TopBalk/Topbalk.png").getImage();

setLayout(null);

setSize(new Dimension(1000, 50));
setBackground(Color.GRAY);

setButtons();
}

public void setButtons() {
//initialise all buttons
playButton = new topMenuButton("src/game/images/TopBalk/Play_Button.png", "src/game/images/TopBalk/PlayGlow_Button.png", 110, 9, 43, 48, 0, "", 30, 34);
forwardButton = new topMenuButton("src/game/images/TopBalk/Forward_Button.png", "src/game/images/TopBalk/ForwardGlow_Button.png", 150, 9, 59, 48, 0, "", 30, 34);
menuButton = new topMenuButton("src/game/images/TopBalk/Menu_Button.png", "src/game/images/TopBalk/MenuGlow_Button.png", 20, 4, 109, 48, 0, "", 38, 86);
goToTheGameButton = new topMenuButton("src/game/images/TopBalk/goToTheGame_Button_needsRework.gif", "src/game/images/TopBalk/goToTheGameGlow_Button_needsRework.gif", 400, 4, 109, 48, 0, "", 38, 86);

//add actionlisteners to buttons
playButton.addActionListener(this);
forwardButton.addActionListener(this);
menuButton.addActionListener(this);
goToTheGameButton.addActionListener(this);

//add buttons that are needed now
addUsefulButtons();
}

public void addUsefulButtons() {
//add the usefull buttons

if (playingNow) {
add(playButton);
add(forwardButton);
}
if (inTheMenu) {
add(goToTheGameButton);
}
if(!inTheMenu){
add(menuButton);
}
if(changingSettings)
{

}
}

public void removeButtons() {
remove(playButton);
remove(forwardButton);
remove(menuButton);
}

@Override
public void actionPerformed(ActionEvent e) {
//If the player hits play or pause
if (e.getSource().toString().contains("Play") || e.getSource().toString().contains("Pauze")) {
//change the pause state of the game
GameLoop.paused = !GameLoop.paused;
//check if the game is paused
if (GameLoop.paused)
//change the play button image
playButton.setImage("src/game/images/TopBalk/Pauze_Button.png", "src/game/images/TopBalk/PauzeGlow_Button.png");
else {
//change the play button image
playButton.setImage("src/game/images/TopBalk/Play_Button.png", "src/game/images/TopBalk/PlayGlow_Button.png");
}
//if the player hits fast forward
} else if (e.getSource().toString().contains("Forward")) {
//do stuff to fast forward
System.out.println("Forward");
}
//if the player hits the menu button
else if (e.getSource().toString().contains("Menu_Button.png")) {
//do stuff to show the menu
System.out.println("Menu");
}
//if the goToTheGame button is pressed
else if (e.getSource().toString().contains("goToTheGame")){
welcomePanel.startGame();
}
//if there is no recognised action command
else{

//just display a message in the console.. WTF??
System.out.println("TopMenuActionlistener has unknown potentials!! Check the actionPerformed plz..");
}
}

public void paintComponent(Graphics g) {
g.drawImage(background, 0, 0, null);
}
}

最佳答案

验证组件意味着布置其子组件,如果需要移动,子组件将重新绘制它们。由于您使用的是绝对定位而不是布局管理器,因此这不会为您做任何事情。

不要调用revalidate(),而是调用repaint()

关于java - 从 JPanel 中删除 JPanel - 仅适用于其自己的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27193589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com