Java, two JPanel on JFrame - Settings JPanel, StartMenu JPanel [on hold]
- by Andy Tyurin
There is my first question and I welcome community!
I'm making a simple game and have some problems with Start menu.
I have three buttons on my JPanel StartMenu and when I click "Settings" button, new JPanel will be open, but I don't know why buttons from StartMenu JPanel appeared in my Settings JPanel.
My "Settings" JPanel has one ugly button "Back" in center and ugly grey background.
I made some screens to see a problem.
Start Menu JPanel when game launched
Settings JPanel when button clicked
Settings JPanel when mouse was over settings window
There is code of StartMenu class:
public class StartMenu extends JPanel {
    private GameButton startGameButton = new GameButton("Start game");
    private GameButton settingsGameButton = new GameButton("Settings");
    private GameButton exitGameButton = new GameButton("Exit game");
    private Image bgImage = new ImageIcon(getClass().getClassLoader().getResource("ru/andydevs/astraLaserForce/bg.png")).getImage();
    private int posX;
    private int posY;
    final private int WIDTH=(int)Game.SCREEN_DIMENSION.getWidth()/3;
    final private int HEIGHT=(int)Game.SCREEN_DIMENSION.getHeight()/2;
    public StartMenu() {
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        setSize(new Dimension(WIDTH, HEIGHT));
        posX=(int)Game.SCREEN_DIMENSION.getWidth()/2-WIDTH/2;
        posY=(int)Game.SCREEN_DIMENSION.getHeight()/2-HEIGHT/2;
        setBounds(posX, posY,WIDTH,HEIGHT);
        c.ipadx=95;
        c.ipady=15;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(20,0,0,0);
        c.gridy=0;
        add(startGameButton, c);
        c.gridy=1;
        c.insets = new Insets(20,0,0,0);
        System.out.println(settingsGameButton.getWidth());
        add(settingsGameButton, c);
        c.gridy=2;
        c.insets = new Insets(20,0,0,0);
        add(exitGameButton, c);
        settingsGameButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                GameOptionsPanel gop = new GameOptionsPanel();
                Game.container.add(gop);
                Game.container.setComponentZOrder(gop, 0);
                Game.container.revalidate();
                Game.container.repaint();
            }
        });
        exitGameButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Main.currentGame.stop();
            }
        });
    }
    public void paintComponent(Graphics g) {
        g.drawImage(bgImage,0,0,WIDTH,HEIGHT,null);
    }
}
There is code of Settings JPanel
public class GameOptionsPanel extends GamePanel {
    private GameButton backButton = new GameButton("Back");
    private GameOptionsPanel that;
    public GameOptionsPanel() {
        super((int) (Game.SCREEN_DIMENSION.getWidth()/3), (int) (Game.SCREEN_DIMENSION.getHeight()/2), new Color(50,50,50));
        that=this;
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill=gbc.HORIZONTAL;
        add(backButton);
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Game.container.remove(that);
                Game.container.revalidate();
                Game.container.repaint();
            }
        });
    }
}
I glad to see some suggestions. Thanks.