Hey I need some help with my java game Im making.
I wont to have an menu with 4 options, startgame, highscore, option and quit.

They quit button is easy I just setVisible(false) but when i click on the other buttons I wont to remove the buttons and include new layouts.

Ive tried to use removeALL(); to clear the container and add() a new panel to the container but it doesnt work and Im not sure I can do it that way.

Anyone have some tips or ideas how to make an menu so please share maby Im thinking all wrong I dont know..?

if you do things like removeAll() on a JFrame or JPanel, then after adding new panels, you might need to call revalidate() or validate(). I've told people that before and they've reported back success, so go try it and let me know. If that doesn't work, post all of your code so that I can run it and I'll help.

Edited 6 Years Ago by BestJewSinceJC: n/a

Ive tried to use validate() but I cant se any difference, I probably did something wrong but this is my code Im kinda new to java so the code is what it is and parts like design and the number generator are far from finish.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class Spel extends JFrame implements KeyListener,ActionListener{
	Label label = new Label();
	private Container c;
	private JButton Start, Highscore, Option, quit;
	private Panel Home = new Panel();
	private Panel Game = new Panel();
	private Dimension Button_size = new Dimension(80,30);
	private JLabel L[];
	private JLabel test;

	
	public Spel(){
		c = getContentPane();
		c.setLayout(new BorderLayout());
		
		Game.setLayout(new GridLayout(label.getRows(), label.getColumn()));
		Home.setLayout(new FlowLayout());
 	    c.add(Home, BorderLayout.NORTH);
 	   
 	    
        Start = new JButton("Start Game");
        Start.setPreferredSize(Button_size);
        Start.addActionListener(this);
        Start.setActionCommand("start");	 
 	    Home.add(Start, BorderLayout.LINE_END);
 	    
 	    Highscore = new JButton("Highscore");
        Highscore.setPreferredSize(Button_size);
 	    Highscore.addActionListener(this);
 	    Highscore.setActionCommand("highscore");	 
 	 	Home.add(Highscore, BorderLayout.SOUTH);
 	    
 	    Option = new JButton("Option");
        Option.setPreferredSize(Button_size);
 	    Option.addActionListener(this);
 	   	Option.setActionCommand("option");	 
 	   	Home.add(Option, BorderLayout.SOUTH);
 	    
 	
 	   	
 	    quit = new JButton("Quit");
        quit.setPreferredSize(Button_size);
 	    quit.addActionListener(this);
 	    quit.setActionCommand("quit");	 
 	    Home.add(quit, BorderLayout.SOUTH);
 	    
		
		setSize(label.getWidth(),label.getHight());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 			setVisible(true);
 		    addKeyListener (this) ; 
			addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
			System.exit(0);
			   }
			});
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String s = e.getActionCommand();
		
		if (s.equals("start")){
			
			startGame();
			
			c.remove(Home);			// This is were Im trying to remove Home and add the Game layout
			c.validate();
			
			c.add(Game, BorderLayout.CENTER);
			
			Start.setText("Reset game");
			Start.setActionCommand("reset");
			
		}
		if (s.equals("highscore")){
			
		}
		if (s.equals("option")){
			
		}
		if (s.equals("quit")){
			setVisible(false);
		}
		if (s.equals("reset")){
						
		}
	}
	
	private void startGame(){

		int size = label.getColumn() * label.getRows();
		L = new JLabel[size];
		
		int c = 0;
		int r = 0;

		label.labelFill();
		for (int i = 1; i < size; i++){
			
			L[i] = new JLabel();
			L[i].setText(""+label.getLabel(r, c));
			Game.add(L[i]);
			c++;
			if (c == label.getColumn()){
				c = 1;
				r++;
			}
		}
	}
	
	public static void main (String[]args ){  
		new Spel();
		
	}

}
import java.util.Random;


public class Label {
	Random rand = new Random();
	private int column = 4;
	private int rows = 4;
	private static int[][] labels;	
	
	
	public Label(){
		labels = new int[5][5];
		labels[4][4] = 16;
	}
	
	public int test(){
		return labels[4][4];
	}
	
	public void setsize(int c, int r){
		column = c;
		rows = r;
	}
	
	public int getHight(){
		int hight = rows * 100;
		return hight;
	}
	
	public int getWidth(){
		int width = column * 100;
		return width;
	}
	
	public int getColumn(){
		return column;
	}
	
	public int getRows(){
		return rows;
	}
	
	public void labelSize(){
		labels = new int[rows][column];
	}
	
	public void labelFill(){
		
		int x = 0;
		int r = 0;
		int c = 0;
		int b = 0;
		int size = column * rows;
		int temp[] = new int[size];
		
		for(int i = 1; i < size; i++){
			
			
			while( b < 5 ){
			x = rand.nextInt(16) + 1;
			
				if ( temp[x] != x ){
					temp[x] = x;
					b = 10;
				}
			}		
			labels[r][c] = x;
			r++;
			if (r == column){
				r = 1;
				c++;
			}
			
		}
	}
	
	public int getLabel(int c, int r){
		return labels[r][c];
	}
	

}

Move the validate() call down after you add the "Game" component

c.add(Game, BorderLayout.CENTER);
			c.validate();
This article has been dead for over six months. Start a new discussion instead.