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
Java Training from DevelopIntelligence
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 07:00 PM
Transparent JFrame bil_Imma AWT / Java Swing 3 26-02-2009 09: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 addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 02:04 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.