Menu, Layout, main program

Hi,

I am writing a game and I am now thinking about writing a sort of menu which is being displayed first when the program has loaded... however, I don't know how I can combine this menu with the GUI I have already written:

first I will use JLabels to display entries like "Start game", "Help", "Exit"... aligned vertically using BoxLayout.

My game is using various classes, the main-class initialises various JPanels and is responsible for the layout.

How can I switch from displaying a menu, which is just 1 JPanel, to displaying my program which is using a combination of some BorderLayouts?

Do you know some good examples?

Thanks! :)

[681 byte] By [SFLa] at [2007-11-26 23:51:46]
# 1
How can I switch from displaying a menu, which is just 1 JPanel, to displaying my program which is using a combination of some BorderLayouts?Just remove() one panel and add() another in its place.
itchyscratchya at 2007-7-11 15:30:51 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks, however, as always, I still have a qestion :)

The API says

"add

public Component add(Component comp)

Appends the specified component to the end of this container (...)"

I have created an example for testing purposes:

the menu:

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.UIManager;

public class Menu extends JComponent implements ActionListener {

private static final long serialVersionUID = 1;

private JButton menuEntry1;

public Menu() {

setLayout(new BorderLayout());

JPanel menuPanel = new JPanel();

menuPanel.setPreferredSize(new Dimension(800, 200));

menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));

menuEntry1 = new JButton("Start new game");

menuEntry1.addActionListener(this);

JButton menuEntry2 = new JButton("Help");

JButton menuEntry3 = new JButton("Exit");

menuPanel.add(menuEntry1);

menuPanel.add(menuEntry2);

menuPanel.add(menuEntry3);

JPanel bluePanel = new JPanel();

bluePanel.setPreferredSize(new Dimension(800, 100));

bluePanel.setBackground(Color.BLUE);

JPanel yellowPanel = new JPanel();

yellowPanel.setPreferredSize(new Dimension(800, 100));

yellowPanel.setBackground(Color.YELLOW);

add(bluePanel, BorderLayout.PAGE_START);

add(menuPanel, BorderLayout.CENTER);

add(yellowPanel, BorderLayout.PAGE_END);

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == menuEntry1) {

System.out.println("New game");

}

}

private static void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(false);

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch(Exception e) {

e.printStackTrace();

}

JFrame frame = new JFrame();

frame.setResizable(false);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Menu newContentPane = new Menu();

newContentPane.setOpaque(true);

frame.setContentPane(newContentPane);

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

and the GUI of the game:

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JToolBar;

import javax.swing.UIManager;

public class MyGame extends JComponent {

private static final long serialVersionUID = 1;

public MyGame() {

setLayout(new BorderLayout());

Dimension dim_greenPanel = new Dimension(800, 600);

Dimension dim_grayPanel = new Dimension(20, 600);

Dimension dim_darkGrayPanel = new Dimension(800, 20);

Dimension dim_cyanPanel = new Dimension(20, 20);

Dimension dim_bluePanel = new Dimension(835, 40);

Dimension dim_magentaPanel = new Dimension(200, 660);

JToolBar toolbar = new JToolBar();

toolbar.setOpaque(true);

toolbar.setFloatable(false);

toolbar.setRollover(true);

JButton aButton = new JButton("Button");

toolbar.add(aButton);

JPanel bluePanel = new JPanel();

bluePanel.setPreferredSize(dim_bluePanel);

bluePanel.setBackground(Color.BLUE);

JPanel cyanPanel = new JPanel();

cyanPanel.setPreferredSize(dim_cyanPanel);

cyanPanel.setBackground(Color.CYAN);

JPanel darkGrayPanel = new JPanel();

darkGrayPanel.setPreferredSize(dim_darkGrayPanel);

darkGrayPanel.setBackground(Color.DARK_GRAY);

JPanel containerPanelNorth = new JPanel(new BorderLayout());

containerPanelNorth.add(bluePanel, BorderLayout.PAGE_START);

containerPanelNorth.add(cyanPanel, BorderLayout.LINE_START);

containerPanelNorth.add(darkGrayPanel, BorderLayout.CENTER);

JPanel grayPanel = new JPanel();

grayPanel.setPreferredSize(dim_grayPanel);

grayPanel.setBackground(Color.GRAY);

JPanel greenPanel = new JPanel();

greenPanel.setPreferredSize(dim_greenPanel);

greenPanel.setBackground(Color.GREEN);

JPanel containerPanelCenter = new JPanel(new BorderLayout());

containerPanelCenter.add(containerPanelNorth, BorderLayout.PAGE_START);

containerPanelCenter.add(grayPanel, BorderLayout.LINE_START);

containerPanelCenter.add(greenPanel, BorderLayout.CENTER);

containerPanelCenter.setOpaque(true);

JPanel magentaPanel = new JPanel();

magentaPanel.setPreferredSize(dim_magentaPanel);

magentaPanel.setBackground(Color.MAGENTA);

add(toolbar, BorderLayout.PAGE_START);

add(containerPanelCenter, BorderLayout.CENTER);

add(magentaPanel, BorderLayout.LINE_END);

}

private static void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(false);

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch(Exception e) {

e.printStackTrace();

}

JFrame frame = new JFrame();

frame.setResizable(false);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MyGame newContentPane = new MyGame();

newContentPane.setOpaque(true);

frame.setContentPane(newContentPane);

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

I am now unsure how I can come from the menu to the GUI of the game if the user clicks the button "Start new game". In which order do I have to add and remove the JPanels?

SFLa at 2007-7-11 15:30:51 > top of Java-index,Desktop,Core GUI APIs...
# 3

The way I would do it (I guess) would be to use CardLayout, and have the game on another card. Simply flip up the gamepanel card when the user wants to play the game. You also have the ability to go back to the menu when the user pauses the game, if that is desirable, since the menu isn't disposed off.

alienchilda at 2007-7-11 15:30:51 > top of Java-index,Desktop,Core GUI APIs...
# 4
Hmmm... I am using 2 different layouts: the menu layout has 3 JPanels arranged top-down. The GUI of the game has a more complex layout. What and how should I flip?
SFLa at 2007-7-11 15:30:51 > top of Java-index,Desktop,Core GUI APIs...
# 5

> What and how should I flip?

We can't answer that, you are designing the game you decide what you want to flip.

A CardLayout can be used on any panel, not just your content pane.

[url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Card Layout[/url].

camickra at 2007-7-11 15:30:51 > top of Java-index,Desktop,Core GUI APIs...
# 6

> We can't answer that, you are designing the game you

> decide what you want to flip.

Solved it.

To go from Menu to MyGame (see code above) I simply call removeAll() and validate() in the actionPerformed method to remove all JPanels from Menu, then I am adding all the other JPanels from my other class MyGame and call validate() again. Seems to work :)

SFLa at 2007-7-11 15:30:51 > top of Java-index,Desktop,Core GUI APIs...