Go Back   Java Programming Forums > Java Standard Edition Programming Help > AWT / Java Swing

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 30-06-2009, 06:25 PM
bruno88's Avatar
Junior Member
 

Join Date: Jun 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
bruno88 is on a distinguished road
Default Change JFrame components problem

Hello guys,

I'm programming a little game and tried to create a menu for it. The menu is like this:



New Game, Load and Exit are JButtons.

When I run the program I see that screen but when i press New Game I want the menu JPanel to be replaced for the game JPanel. I was able to do it the problem is that the KeyListener on my Game panel doens't work. Any way i can do it having the KeyListener working when menu JPanel is removed?

Java Code
public class GameFrame extends JFrame{

private GameMenu menu;
private GameCanvas canvas;

public GameFrame(String name, int width, int height) {
        super(name);
        
        menu = new GameMenu(this, width,height);
        add(menu);
        
        canvas = new GameCanvas(width, height);

        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
}

//When New Game button is pressed this metod is called.
protected void beginGame() {
        
        setContentPane(canvas);
        canvas.launchGame();
        canvas.repaint();
        validate();
}
}

public class GameCanvas extends JPanel implements KeyListener{

protected GameCanvas(int width, int height) {
        super();
        this.width = width;
        this.height = height;
        setPreferredSize(new Dimension(width, height));

        addKeyListener(this);
        setFocusable(true);
}
}
Please help me!



Reply With Quote Share this thread on Facebook
Sponsored Links
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] simple change to a boolean value in a method call help... rptech Object Oriented Programming 3 20-05-2009 10:48 AM
Missing pieces of components (JFileChooser, JButtons, etc) help? jlobe AWT / Java Swing 2 06-03-2009 08:00 PM
Transparent JFrame bil_Imma AWT / Java Swing 3 26-02-2009 10:21 PM
How to Change JTextArea font, font size and color Flash Java Code Snippets and Tutorials 4 08-07-2008 06:45 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java convert double to int java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java jbutton java list to map java mouse position java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java jtextarea font size programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 05:37 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.