Results 1 to 6 of 6
Thread: Swing Basics
- 01-21-2011, 03:57 AM #1
Member
- Join Date
- Aug 2008
- Location
- Boston
- Posts
- 11
- Rep Power
- 0
Swing Basics
I am having trouble when trying to add two JPanel objects to a JFrame. Below are my classes, and for some reason when my JFrame loads, nothing is drawn in it. The code is unfinished, but included in it's entirety to give a clearer picture of what I'm creating. I have only just begun my implementation and can't figure out why my panel with the buttons isn't being added to my GameWindow. Thanks in advance for any help.
Java Code:public class Pong { public Pong() { GameWindow window = new GameWindow(); window.loadGame(); } public static void main(String[] args) { new Pong(); } }Java Code:import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JFrame; public class GameWindow extends JFrame { // Default Serial Version UID private static final long serialVersionUID = 1L; public GameWindow() { this(Preferences.MEDIUM_WINDOW_SIZE); } public GameWindow(Dimension windowSize) { setTitle("Bouncing Balls"); setSize(windowSize); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } void loadGame() { GameControlPanel gameInformationPanel = new GameControlPanel(); GamePanel gamePanel = new GamePanel(); Ball ball = new Ball(); gamePanel.addBall(ball); this.setLayout(new BorderLayout()); this.add(gameInformationPanel, BorderLayout.NORTH); this.add(gamePanel, BorderLayout.CENTER); } }Java Code:import java.awt.FlowLayout; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JPanel; public class GameControlPanel extends JPanel { // Default Serial Version UID private static final long serialVersionUID = 1L; public GameControlPanel() { JButton startButton = new JButton("Start"); JButton stopButton = new JButton("Stop"); this.setLayout(new FlowLayout()); this.add(startButton); this.add(stopButton); } public void paint(Graphics g) { super.paint(g); } }Java Code:import java.util.ArrayList; import javax.swing.JPanel; public class GamePanel extends JPanel { // Default Serial Version UID private static final long serialVersionUID = 1L; ArrayList<Ball> gameBalls = new ArrayList<Ball>(); public GamePanel() { } void addBall(Ball ball) { gameBalls.add(ball); } }
- 01-21-2011, 04:00 AM #2
You are adding the panels after you have made the frame visible. Try resizing your frame. If your panels show up after that then this is an indication that you are doing things in the wrong order.
- 01-21-2011, 04:17 AM #3
Member
- Join Date
- Aug 2008
- Location
- Boston
- Posts
- 11
- Rep Power
- 0
Right you were, thanks Junky. I added a paint function to my JFrame and performed a call to repaint() after adding the two panels. It now shows up correctly. Thank you.
- 01-21-2011, 04:22 AM #4
Member
- Join Date
- Aug 2008
- Location
- Boston
- Posts
- 11
- Rep Power
- 0
For completion sake if anyone chooses to look at this thread, below is the code for the revised GameWindow Class:
Java Code:import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JFrame; public class GameWindow extends JFrame { // Default Serial Version UID private static final long serialVersionUID = 1L; public GameWindow() { this(Preferences.MEDIUM_WINDOW_SIZE); } public GameWindow(Dimension windowSize) { setTitle("Bouncing Balls"); setSize(windowSize); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } void loadGame() { GameControlPanel gameInformationPanel = new GameControlPanel(); GamePanel gamePanel = new GamePanel(); Ball ball = new Ball(); gamePanel.addBall(ball); this.setLayout(new BorderLayout()); this.add(gameInformationPanel, BorderLayout.NORTH); this.add(gamePanel, BorderLayout.CENTER); repaint(); } public void paint(Graphics g) { super.paintComponents(g); } }
- 01-21-2011, 06:52 AM #5
Wrong approach. The preferred approach is to add components to a top-level window before, not after, it is shown, and if it becomes necessary to add/remove components after the frame is visible, you should call pack().
In the case of adding/removing components to/from a lightweight container (JComponent subclass) the correct method to call is revalidate() followed by repaint()
db
- 01-21-2011, 06:59 AM #6
Member
- Join Date
- Aug 2008
- Location
- Boston
- Posts
- 11
- Rep Power
- 0
Thanks for the input Darryl. It's 2:00 AM and I'll be going to bed before I have a chance to play around with a new implementation. I'll do my best to have revised code posted over the weekend, and would love your feedback once again to let me know if I'm headed in the right direction.
Thanks,
Jared
Similar Threads
-
the very very very basics of java2d
By senca in forum New To JavaReplies: 1Last Post: 11-24-2010, 03:30 PM -
Basics
By avish12 in forum SWT / JFaceReplies: 2Last Post: 06-09-2010, 03:04 PM -
Really Basics
By Taluntain in forum New To JavaReplies: 16Last Post: 10-08-2009, 09:43 AM -
About swing basics
By harithaspa in forum AWT / SwingReplies: 2Last Post: 12-13-2008, 12:07 PM -
Basics
By AKP in forum New To JavaReplies: 7Last Post: 05-23-2008, 12:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks