ok, i have a problem with JLayeredPane. I'm about to make a game in full screen mode, so basically I create a JFrame first, and I named it MainMenu.java. In this JFrame, I first display my main menu, where there are 4 main buttons there. When the button is click, the mainmenu is supposed to disappear and replaced with the related menu. To make it work, I create a JLayeredPane named Panel that contains everything for the mainmenu. To open the menu, I simply nullify the Panel and add new JLayeredPane to fills the Panel. To make it easy, I separate the submenu class into SubMenu.java. My problem is, when I first run the program, there's nothing in the frame (there's supposed to be a JLayeredPane named Panel that contains the mainmenu). Then I minimize the window, and maximize it again. Boom, I got the main menu I want. Then I try to open the menu in it, and everything freezes. I minimize and maximize the window again, and I got the menu I want. Can someone here please tell me what's wrong? It seems like the Panel can't refresh whenever I change it.
Here's the code for MainMenu.java and SubMenu.java. You can ignore most part of the code, since it's still basic for the button animation. But please pay attention with the Panel.
// Java Foundation Classes (JFC)
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class MainMenu extends JFrame implements MouseListener
{
private ImageIcon BackgroundIco;
private ImageIcon MenuGameIco;
private ImageIcon MenuOptionIco;
private ImageIcon MenuHelpIco;
private ImageIcon MenuExitIco;
private JLabel MenuGame;
private JLabel MenuOption;
private JLabel MenuHelp;
private JLabel MenuExit;
private JLabel Background;
private JLayeredPane Panel;
private SubMenu ActiveMenu;
private GameMonopoli a;
public MainMenu()
{
setVisible(true);
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setDisplayMode(new DisplayMode(1024, 768, 32, 60));
validate();
ActiveMenu = null;
Panel = new JLayeredPane();
Panel.setLayout(null);
Panel.setVisible(true);
BackgroundIco = new ImageIcon("resource/background.png");
Background = new JLabel(BackgroundIco);
Background.setBounds(0,0,BackgroundIco.getIconWidth(),BackgroundIco.getIconHeight());
MenuGameIco = new ImageIcon("resource/play.png");
MenuOptionIco = new ImageIcon("resource/option.png");
MenuHelpIco = new ImageIcon("resource/help.png");
MenuExitIco = new ImageIcon("resource/exit.png");
MenuGame = new JLabel(MenuGameIco);
MenuOption = new JLabel(MenuOptionIco);
MenuHelp = new JLabel(MenuHelpIco);
MenuExit = new JLabel(MenuExitIco);
MenuGame.setBounds(670, 80, MenuGameIco.getIconWidth(), MenuGameIco.getIconHeight());
MenuOption.setBounds(670,230, MenuOptionIco.getIconWidth(), MenuOptionIco.getIconHeight());
MenuHelp.setBounds(670,380, MenuHelpIco.getIconWidth(), MenuHelpIco.getIconHeight());
MenuExit.setBounds(670,530, MenuExitIco.getIconWidth(), MenuExitIco.getIconHeight());
//mouselistener
MenuGame.addMouseListener(this);
MenuOption.addMouseListener(this);
MenuHelp.addMouseListener(this);
MenuExit.addMouseListener(this);
loadMainMenu();
//SubMenu asdf = new SubMenu();
//Panel=asdf.getPanel();
//add(Panel);
}
public void mousePressed(MouseEvent e)
{
if (e.getSource()==MenuGame)
{
MenuGame.setIcon(new ImageIcon("./resource/playPressed.png"));
}
else if(e.getSource()==MenuOption)
{
MenuOption.setIcon(new ImageIcon("./resource/optionPressed.png"));
}
else if(e.getSource()==MenuHelp)
{
MenuHelp.setIcon(new ImageIcon("./resource/helpPressed.png"));
}
else if(e.getSource()==MenuExit)
{
MenuExit.setIcon(new ImageIcon("./resource/exitPressed.png"));
}
}
public void mouseReleased(MouseEvent e)
{
if (e.getSource()==MenuGame)
{
MenuGame.setIcon(new ImageIcon("./resource/playHover.png"));
}
else if(e.getSource()==MenuOption)
{
MenuOption.setIcon(new ImageIcon("./resource/optionHover.png"));
}
else if(e.getSource()==MenuHelp)
{
MenuHelp.setIcon(new ImageIcon("./resource/helpHover.png"));
}
else if(e.getSource()==MenuExit)
{
MenuExit.setIcon(new ImageIcon("./resource/exitHover.png"));
}
}
public void mouseEntered(MouseEvent e)
{
if (e.getSource()==MenuGame)
{
MenuGame.setIcon(new ImageIcon("./resource/playHover.png"));
}
else if(e.getSource()==MenuOption)
{
MenuOption.setIcon(new ImageIcon("./resource/optionHover.png"));
}
else if(e.getSource()==MenuHelp)
{
MenuHelp.setIcon(new ImageIcon("./resource/helpHover.png"));
}
else if(e.getSource()==MenuExit)
{
MenuExit.setIcon(new ImageIcon("./resource/exitHover.png"));
}
}
public void mouseExited(MouseEvent e)
{
if (e.getSource()==MenuGame)
{
MenuGame.setIcon(new ImageIcon("./resource/play.png"));
}
else if(e.getSource()==MenuOption)
{
MenuOption.setIcon(new ImageIcon("./resource/option.png"));
}
else if(e.getSource()==MenuHelp)
{
MenuHelp.setIcon(new ImageIcon("./resource/help.png"));
}
else if(e.getSource()==MenuExit)
{
MenuExit.setIcon(new ImageIcon("./resource/exit.png"));
}
}
public void mouseClicked(MouseEvent e)
{
if(e.getSource()==MenuGame)
{
}
else if(e.getSource()==MenuOption)
{
ActiveMenu = new SubMenu();
loadOptionHelp();
}
else if(e.getSource()==MenuHelp)
{
ActiveMenu = new SubMenu();
loadOptionHelp();
}
else if(e.getSource()==MenuExit)
{
this.dispose();
}
}
public JLayeredPane getPanel()
{
return Panel;
}
private void loadMainMenu()
{
Panel = new JLayeredPane();
Panel.add(Background);
Panel.add(MenuGame);
Panel.add(MenuOption);
Panel.add(MenuHelp);
Panel.add(MenuExit);
Panel.moveToBack(Background);
Panel.moveToFront(MenuGame);
Panel.moveToFront(MenuOption);
Panel.moveToFront(MenuHelp);
Panel.moveToFront(MenuExit);
add(Panel);
}
private void loadOptionHelp()
{
remove(Panel);
Panel = ActiveMenu.getPanel();
add(Panel);
}
public static void main(String[] args)
{
MainMenu a = new MainMenu();
}
}
import java.awt.Image;
import java.awt.Color;
import javax.swing.ImageIcon;
import java.awt.Dimension;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.Container;
import java.awt.*;
import javax.swing.*;
public class SubMenu
{
private JLayeredPane Panel;
public SubMenu()
{
Panel = new JLayeredPane();
Panel.setVisible(true);
//Panel.setPreferredSize(new Dimension(WIDTH,HEIGHT));
//setLayout(new BorderLayout());
ImageIcon headerIco = new ImageIcon("resource/header.png");
ImageIcon footerIco = new ImageIcon("resource/footer.png");
ImageIcon buttonIco = new ImageIcon("resource/button.jpg");
JLabel header = new JLabel(headerIco);
//JLayeredPane footer = new JLayeredPane();
JLabel footerLabel = new JLabel(footerIco);
JLabel button = new JLabel(buttonIco);
//footer.add(footerLabel);
//footer.add(button);
Panel.add(header);
Panel.add(footerLabel);
Panel.add(button);
Panel.moveToFront(button);
Panel.moveToBack(footerLabel);
header.setBounds(0,0,headerIco.getIconWidth(),headerIco.getIconHeight());
footerLabel.setBounds(0,500,footerIco.getIconWidth(),footerIco.getIconHeight());
button.setBounds(825,513,buttonIco.getIconWidth(),buttonIco.getIconHeight());
//add(header, BorderLayout.NORTH);
//add(footer, BorderLayout.CENTER);
}
public void addContent(JLayeredPane content)
{
content.setBounds(0,200,content.getWidth(),content.getHeight());
Panel.add(content);
}
public JLayeredPane getPanel()
{
return Panel;
}
/*public static void main(String[] args)
{
SubMenu a = new SubMenu ();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setSize(1024,768);
frame.add(a.getPanel(),BorderLayout.CENTER);
frame.setVisible(true);
}*/
}
Thanks in advance.
Regards,
AG