Results 1 to 6 of 6

Thread: Swing Basics

  1. #1
    JCollier is offline Member
    Join Date
    Aug 2008
    Location
    Boston
    Posts
    11
    Rep Power
    0

    Default 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);
    	}
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  3. #3
    JCollier is offline Member
    Join Date
    Aug 2008
    Location
    Boston
    Posts
    11
    Rep Power
    0

    Default

    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.

  4. #4
    JCollier is offline Member
    Join Date
    Aug 2008
    Location
    Boston
    Posts
    11
    Rep Power
    0

    Default

    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);
    	}
    }

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Quote Originally Posted by JCollier View Post
    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.
    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

  6. #6
    JCollier is offline Member
    Join Date
    Aug 2008
    Location
    Boston
    Posts
    11
    Rep Power
    0

    Default

    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

  1. the very very very basics of java2d
    By senca in forum New To Java
    Replies: 1
    Last Post: 11-24-2010, 03:30 PM
  2. Basics
    By avish12 in forum SWT / JFace
    Replies: 2
    Last Post: 06-09-2010, 03:04 PM
  3. Really Basics
    By Taluntain in forum New To Java
    Replies: 16
    Last Post: 10-08-2009, 09:43 AM
  4. About swing basics
    By harithaspa in forum AWT / Swing
    Replies: 2
    Last Post: 12-13-2008, 12:07 PM
  5. Basics
    By AKP in forum New To Java
    Replies: 7
    Last Post: 05-23-2008, 12:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •