0

i coded a game in java (the crome offline game but with my own textures). The the game screen incoded in a class that extends a jPanel and it is called by a different class that extends a jFrame. The other screens, e.g. highscore screen and the title screen were created in a jPanel form design in NetBeans IDE 8.2 (it allowed me design the gui's by dragging and droping while it made uneditable auto-generated code for the Panels) Each jPanel form it's own class and as i did with the game screen called by the JFrame class by instantiating and using:

add(*jPanelName*);

i did this for all my screens and to switch between them ive been closing the window, commenting out all panels excluding the one i want to see when i run and clicking run again.

how can i switch between them using thier respective buttons?

im sorry for my post being so long and for if im not punctual.

i tried a switch statment that would change depending on a variable (currentScreen) that i made public in hopes that if i changed it upon clicking a button, it would trigger the switch statment and the screen would change. this is what the code in the buttons looks like:

private void jHighScoresActionPerformed(java.awt.event.ActionEvent evt) {                                       
        GameWindow.currentScreen = 2;
        System.out.println(GameWindow.currentScreen); //confirms if it changes when clicked (it does)

the attenpted switch statment:

    private PlayScreen playScreen;
    private TitleScreen titleScreen;
    private HighScoreScreen highScoreScreen;
    
    private final int TITLE = 0;
    private final int PLAY = 1;
    private final int HISHSCORE = 2;
    
    public static int currentScreen;

public GameWindow() {
        ...code to set the frame...
        
        
        playScreen = new PlayScreen();
        titleScreen = new TitleScreen();
        highScoreScreen = new HighScoreScreen();
        
                highScoreScreen.setVisible(false);
                titleScreen.setVisible(false);
                playScreen.setVisible(false);

        setScreen();
    }


public void setScreen(){
        switch(currentScreen){
            case TITLE:
                remove(playScreen);
                remove(highScoreScreen);
                add(titleScreen);
                playScreen.setVisible(true);
            case PLAY:
                remove(titleScreen);
                add(playScreen);
                addKeyListener(playScreen);
                titleScreen.setVisible(false);
                playScreen.setVisible(true);
                break;
            case HISHSCORE:
                remove(titleScreen);
                add(highScoreScreen);
                titleScreen.setVisible(false);
                playScreen.setVisible(true);
                break;
        }
    }

0 Answers0