Results 1 to 10 of 10
- 01-16-2011, 08:52 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
Can't figure out how to use JFrame's add() method right
I'm trying to make a game with multiple levels, but once I load the second level I can't move my character on screen. I know where the problem is, but I don't fully know what's going on or how to fix the problem.
The problem is adding the classes "Inner" and "Inner2" to the JFrame. I can add the variable "inner" to the JFrame easily enough by putting it at the beginning of the constructor right after the call for super(); but if I put it anywhere else I am not able to move in my game.
Everything was working just fine until I needed more than one level, so I added the Swing timer to constantly check for when the player went over a certain limit, where the game was then supposed to remove the first level from the JFrame and then add the second level.
I tried this and I found myself not able to move in the game.
Java Code:import javax.swing.*; import java.awt.event.*; import java.awt.*; public class ProjectWindow extends JFrame implements ActionListener { public Inner inner; public Inner2 inner2; public boolean level1; public Timer timer; private int check = 0; private boolean level1unf = true; ProjectWindow() { super(); inner = new Inner(); add(inner); // If I put this anywhere else, I can't move in the game setTitle("Civil War Game"); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); setSize(600, 500); setBounds(75,75,600,500); setVisible(true); level1 = true; // if I were to put add(inner); here, it would not let me move in game. timer = new Timer(100, this); } public void Level() { if(level1) { level1 = false; timer.start(); } } public void actionPerformed(ActionEvent e) { check = inner.getInt(); if(check > 1500 && level1unf) { level1unf = false; remove(inner); inner2 = new Inner2(); add(inner2); // this is where the issue is. Because I can't add //everything in the constructor, I have to add the second level here. // The game will basically freeze and I won't be able to move. } } }Last edited by Fortu; 01-16-2011 at 09:43 PM.
-
Whenever you add and remove components, you need to revalidate() and often repaint() the container that has had components changed. If this Container is the contentPane, you may need to cast it to a JPanel before calling revalidate() on it. To make things easier for you, I would suggest you use a CardLayout to allow you to more easily swap components (JPanels) in your JFrame. As for your specific problem, I'm not sure how we can guess what the problem is given the data at hand.
- 01-16-2011, 09:50 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
Well, I tried to repaint() and validate() like you said, but all I can do is fire bullets when I remove inner and add inner2.
I still can't move when inner2 is added, but mouseEvents are seen by Inner2, which extends JPanel, which means I can see the bullet as it is painted on the screen, but my keyPressed and keyReleased events in Inner2 don't seem to be doing anything.
In short, I can shoot bullets, see them on screen, kill enemies successfully with them, and hear the audio that plays when I shoot, but I can not move.
Java Code:import javax.swing.*; import java.awt.event.*; import java.awt.*; public class ProjectWindow extends JFrame implements ActionListener { public Inner inner; public Inner2 inner2; public boolean level1; public Timer timer; private int check = 0; private boolean level1unf = true; ProjectWindow() { super(); inner = new Inner(); getContentPane().add(inner); inner2 = new Inner2(); setTitle("Civil War Game"); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); setSize(600, 500); setBounds(75,75,600,500); setVisible(true); level1 = true; timer = new Timer(100, this); } public void Level() { if(level1) { level1 = false; timer.start(); } } public void actionPerformed(ActionEvent e) { check = inner.getInt(); if(check > 1500 && level1unf) { level1unf = false; getContentPane().remove(inner); inner2 = new Inner2(); getContentPane().add(inner2); getContentPane().validate(); getContentPane().repaint(); } } }
-
I'm stumped by a lack of understanding of the workings of your program. You say "you can't move", but we see no code that makes you move. You're not using KeyListeners by chance are you? If so, you may have a problem of lack of focus since KeyListeners only work on the component that has focus. To get around this, you may try to use key binding instead. Please note that this recommendation is just a guess since we're only privy to a partial information about the problem.
- 01-16-2011, 10:07 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
Yes, I am using KeyListeners, and I will check out key binding right now.
(Sidenote: I thought using a KeyListener was the only way to do this. o_o)
- 01-16-2011, 10:29 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
Here's a small example of what Inner and Inner2 have for KeyListeners and MouseListeners:
Java Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.ArrayList; public class Inner extends JPanel implements ActionListener { public Player playuh; Inner() { super(); setBackground(Color.BLACK); setFocusable(true); addKeyListener(new TAdapter()); addMouseListener(new RAdapter()); playuh = new Player(); setDoubleBuffered(true); setSize(600,465); } ... class TAdapter extends KeyAdapter { public void keyReleased(KeyEvent e) { playuh.keyReleased(e); } public void keyPressed(KeyEvent e) { playuh.keyPressed(e); } } class RAdapter extends MouseAdapter { public void mousePressed(MouseEvent e) { playuh.mousePressed(e); } public void mouseReleased(MouseEvent e) { } }
-
So if you click on your GUI after the view changes, do the key listeners work? I assume that they do. If so, again consider using key binding instead.
- 01-16-2011, 10:36 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
-
- 01-17-2011, 12:14 AM #10
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
Similar Threads
-
disabling JFrame's response to spacebar
By gib65 in forum AWT / SwingReplies: 2Last Post: 11-01-2010, 04:38 PM -
Cant figure out where went wrong.
By leviathan in forum New To JavaReplies: 15Last Post: 06-06-2010, 06:55 PM -
Need help - I can't figure it out.
By Joshsmith in forum New To JavaReplies: 2Last Post: 10-23-2009, 10:12 PM -
Need Help in BlueJ... Cannot figure this out.
By ERICAMAUVE4 in forum New To JavaReplies: 2Last Post: 09-25-2009, 12:34 AM -
I can't figure this out
By silvia in forum New To JavaReplies: 3Last Post: 07-20-2007, 04:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks