Results 1 to 5 of 5
  1. #1
    floris is offline Member
    Join Date
    Apr 2012
    Location
    Den Haag, the Netherlands
    Posts
    5
    Rep Power
    0

    Default multiple JPanels on a Jframe

    Hello. This topic, I believe so, is basic OOP related.
    I am trying to add a few JPanels on certain positions and size 50x50.
    I noticed that the first objects appear how they should and everytime the latest shows fullscreen. I really can't figure out why that is happening. Maybe someone can tell me what is happening in the code?
    (BTW I added some mouseactions for testing purposes).

    The code for the frame:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.JFrame;
    
    
    public class RunTest extends JFrame 
    {
    
    	public RunTest(){
    		Vak eenVak = new Vak(50,50, Color.cyan, "cyaan");
    		Vak nogEenVak = new Vak(200,200, Color.green, "groen");
    		Vak derdeVak = new Vak(400,200, Color.black, "zwart");
    		Vak vierdeVak = new Vak(500, 500, Color.red, "rood");
    		
    		JFrame venster = new JFrame("testvenster");
    		venster.setLocation(100, 100);
    		venster.setSize(800, 800);
    		venster.setLayout(new BorderLayout() );
    		venster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		venster.add(eenVak);
    		venster.add(nogEenVak);
    		venster.add(derdeVak);
    		venster.add(vierdeVak);
    		venster.setVisible(true);
    	}
    	
    	public static void main(String[] args)
    	{
    		new RunTest();		
    	}
    }
    The code for the JPanel object:
    Java Code:
    import java.awt.Color;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JPanel;
    
    
    public class Vak extends JPanel implements MouseListener
    {
    	
    	private int xLocatie;
    	private int yLocatie;
    	private String naam;
    	
    	public Vak(int xLocatie, int yLocatie, Color achtergrond, String naam)
    	{
    		System.out.println("Vak instantie is aangemaakt");
    		this.xLocatie = xLocatie;
    		this.yLocatie = yLocatie;
    		this.naam = naam;
    		
    		this.setBackground(achtergrond);
    		this.setSize(50,50);
    		this.setLocation(xLocatie, yLocatie);
    		
    		this.setBounds(xLocatie, yLocatie, 50, 50);
    		setLayout(null);
    		addMouseListener(this);
    		
    			
    	}
    	
    	public int getxLocatie()
    	{
    		return xLocatie;
    	}
    
    	public void setxLocatie(int xLocatie)
    	{
    		this.xLocatie = xLocatie;
    	}
    
    	public int getyLocatie()
    	{
    		return yLocatie;
    	}
    
    	public void setyLocatie(int yLocatie)
    	{
    		this.yLocatie = yLocatie;
    	}
    
    	public void mouseClicked(MouseEvent e)
    	{
    		System.out.println(naam +" zegt:  OUCH");
    		this.setxLocatie(xLocatie + 100);
    		this.setxLocatie(yLocatie + 100);
    		repaint();
    		
    	}
    
    	public void mouseEntered(MouseEvent e)
    	{
    		System.out.println(naam +" zegt:  HELLO");
    	}
    
    	public void mouseExited(MouseEvent e)
    	{
    		System.out.println(naam +" zegt:  GOOGBYE");
    		
    	}
    
    	public void mousePressed(MouseEvent e)
    	{
    		
    		
    	}
    
    	public void mouseReleased(MouseEvent e)
    	{
    		
    		
    	}
    
    }
    Thanks in advance!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: multiple JPanels on a Jframe

    Moved from New to Java.

    Not OOP related at all. It's your lack of knowledge about the working of layout managers. Go through this Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    BorderLayout manages only one Component in each of its 5 zones. The components you added earlier aren't managed by the layout so their display is gooverned by the setSize/setLocation calls in your code.

    Find and use a suitable layout manager from the tutorial linked and get rid of any setSize/setLocation/setBounds calls.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    floris is offline Member
    Join Date
    Apr 2012
    Location
    Den Haag, the Netherlands
    Posts
    5
    Rep Power
    0

    Default Re: multiple JPanels on a Jframe

    I am very sorry for putting it in the wrong topic. (I'm a newbie)

    Do you agree I can use the
    Java Code:
     venster.setLayout(null)
    in the RunTest class?
    And not use any layout manager at all. I am trying to get a more pixel perfect app. and don't need a manager to put things in a sort of (invisible) frame for me.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: multiple JPanels on a Jframe

    In the long run, you're going to have to use layout managers. You can't get far with a null layout. Why delay your learning?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    floris is offline Member
    Join Date
    Apr 2012
    Location
    Den Haag, the Netherlands
    Posts
    5
    Rep Power
    0

    Default Re: multiple JPanels on a Jframe

    Thanks!
    You are a good teacher! ;)

Similar Threads

  1. multiple JPanels on one Jscrollpane
    By Mion in forum AWT / Swing
    Replies: 1
    Last Post: 11-21-2011, 05:52 PM
  2. Multiple JPanels within a JFrame
    By horndinkle in forum New To Java
    Replies: 5
    Last Post: 01-09-2011, 07:51 PM
  3. repainting a jframe containing two jpanels
    By musasabi in forum New To Java
    Replies: 0
    Last Post: 05-12-2010, 04:36 AM
  4. Multiple JPanels Not Working
    By jgezau in forum AWT / Swing
    Replies: 5
    Last Post: 04-17-2009, 04:52 AM
  5. Can't synchronize multiple JPanels in a JFrame
    By vassil_zorev in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2007, 04:22 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
  •