Reputation: 79
So a little earlier, some people advised me to use the CardLayout
for the menu of a Java game.
I read a little about it and started coding. and it works very nice!
I only have this one problem i just can't figure out:
On my first JPanel
I have 3 JButtons
+ a JLabel
(image, my logo), on my 2nd JPanel
, i have the same but with different text on the buttons.
The problem is that my logo doesn't display on startup on the first JPanel
(buttons do), and when i go to the 2nd JPanel
, the logo does appear en buttons to (how it should be)
so it's: Jpanel1
is New game, Highscores
, Exit --> New game leads to 3 difficulty buttons.
short version of my code:
package labyrinthproject.View;
import labyrinthproject.Controller.Control;
import labyrinthproject.model.Spel.Labyrint;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainMenu {
ImageIcon logoImg = new ImageIcon("Resources\\mainlogo.png");
JFrame frame = new JFrame("A-Maze-Ing");
JPanel panelCont = new JPanel();
JPanel panelMenu = new JPanel();
JPanel panelOption = new JPanel();
JButton playButton = new JButton ("Nieuw Spel");
JButton highscoresButton = new JButton ("Highscores");
JButton sluitButton = new JButton ("Sluiten");
JButton maze1 = new JButton("Makkelijk");
JButton maze2 = new JButton("Normaal");
JButton maze3 = new JButton("Hardcore");
JLabel logo = new JLabel();
CardLayout cl = new CardLayout();
public void mainmenu() {
//Cardlayout
panelCont.setLayout(cl);
// 800x600 Frame
frame.setSize(new Dimension(800, 600));
frame.getContentPane().setBackground(new Color(14,36,69));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(frame.getMinimumSize());
//logo
logo.setIcon(logoImg);
logo.setAlignmentX(JComponent.CENTER_ALIGNMENT);
//690x540 Panel ( grootte van map );
Dimension expectedDimension = new Dimension(690, 540);
panelMenu.setLayout(new BoxLayout(panelMenu, BoxLayout.Y_AXIS));
panelMenu.setPreferredSize(expectedDimension);
panelMenu.setMaximumSize(expectedDimension);
panelMenu.setMinimumSize(expectedDimension);
panelMenu.setBackground(new Color(14, 36, 69)); //14, 36, 69
panelMenu.add(logo);
panelMenu.add(playButton);
panelMenu.add(highscoresButton);
panelMenu.add(sluitButton);
sluitButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
//Optionpanel
panelOption.setLayout(new BoxLayout(panelOption,BoxLayout.Y_AXIS));
panelOption.setPreferredSize(expectedDimension);
panelOption.setMaximumSize(expectedDimension);
panelOption.setMinimumSize(expectedDimension);
panelOption.setBackground(new Color(14,36,69));
panelOption.add(logo);
panelOption.add(maze1);
panelOption.add(maze2);
panelOption.add(maze3);
maze1.setAlignmentX(JComponent.CENTER_ALIGNMENT);
maze2.setAlignmentX(JComponent.CENTER_ALIGNMENT);
maze3.setAlignmentX(JComponent.CENTER_ALIGNMENT);
terug.setAlignmentX(JComponent.CENTER_ALIGNMENT);
//maze1
panelMaze1.setBackground(new Color(255,0,0)); //test
//maze2
panelMaze2.setBackground(new Color(0,0,255)); //test
//maze3
panelMaze3.setBackground(new Color(0,255,0)); //test
//playButton
playButton.setFont(new Font("Old English Text MT", Font.BOLD, 40));
playButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cl.show(panelCont, "2");
}
});
//highscorelabel
highscoresButton.setFont(new Font("Old English Text MT", Font.BOLD, 40));
//sluitButton
sluitButton.setFont(new Font("Old English Text MT", Font.BOLD, 40));
sluitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//makkelijk button
maze1.setFont(new Font("Old English Text MT", Font.BOLD, 40));
maze1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cl.show(panelCont, "3");
}
});
//normaal button
maze2.setFont(new Font("Old English Text MT", Font.BOLD, 40));
maze2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cl.show(panelCont, "4");
}
});
//hardcore button
maze3.setFont(new Font("Old English Text MT", Font.BOLD, 40));
maze3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cl.show(panelCont, "5");
}
});
panelCont.add(panelMenu, "1");
panelCont.add(panelOption, "2");
panelCont.add(panelMaze1, "3");
panelCont.add(panelMaze2, "4");
panelCont.add(panelMaze3, "5");
cl.show(panelCont, "1");
frame.add(panelCont, BorderLayout.CENTER);
frame.setVisible(true);
}
}
Thank you in advance! Sincerely a beginner java programmer!
Upvotes: 1
Views: 105
Reputation: 209112
"The problem is that my logo doesn't display on startup on the first JPanel(buttons do), and when i go to the 2nd JPanel, the logo does appear en button"
One thing you have to understand about components is that they can only have on parent container. If you try and the same component twice, only the last container you add it to will receive the component.
You only have one JLabel icon
but you try to add it twice, once to panelMenu
and once to paneOption
, so panelOption
is the only one to receive it.
JLabel logo = new JLabel();
panelMenu.add(logo);
panelOption.add(logo);
Simple fix is just to create two JLabels
. hey can use the same ImageIcon
, as ImageIcon
is not a component.
Upvotes: 2