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

时间:2014-11-28 17:35:06

标签: java swing jpanel

关于这一点有很多问题,但它们都提出了重新验证的东西......我的代码在一个类中起作用,但如果它从另一个类触发则不行。

我有一个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();
}

所以如果 - 在init之后 - 我会调用startGame()它可以工作,它会删除welcomeScreen并添加gameScreen。

如果我从topMenu中的actionlistener调用它,它什么都不做。更新,删除旧面板,或在旧面板上添加新面板。

其他课程:

WelcomeScreen

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(基本上:actionlistener在按钮上调用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);
    }
}

1 个答案:

答案 0 :(得分:3)

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

而不是致电revalidate(),请致电repaint()