Reputation: 9809
There are many questions about this, but they are all coming up with revalidate stuff.. my code works from within one class but doesnt if it gets triggered from another class.
I have a JPanel, i want to add a JPanel to it.
I have a method startGame() witch should remove the panel added to it currently.
The method startGame() works if i call it directly from the constructor.
The method startGame() does not work if i call it from another class.
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();
}
so if - after init - i would call startGame() it works, and it removes the welcomeScreen and adds gameScreen.
If i call that from an actionlistener in topMenu, it doesnt do anything. Neighter remove the old panel, or add the new one over the old panel.
Other classes:
WelcomeScreen
public class WelcomeScreen extends JPanel {
public WelcomeScreen()
{
setBounds(0, 50, GameBase.gameWidth, GameBase.gameHeight - 50);
setBackground(Color.GREEN);
}
}
Game
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 (basically: the actionlistener calls the startGame method on a button - tested and works.)
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);
}
}
Upvotes: 0
Views: 31
Reputation: 50021
Validating a component means laying out its subcomponents, which will then repaint them if they they need to move. Since you are using absolute positioning rather than a layout manager, this isn't going to do anything for you.
Instead of calling revalidate()
, call repaint()
.
Upvotes: 3