Results 1 to 10 of 10
  1. #1
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Post Frame with 2 Panels

    I am trying to create a frame with 2 panels each panel containing three buttons. The panel and frame layout is FlowLayout. When I compile and run I am getting a Red panel with visible buttons 4, 5, 6 and nothing else. I am missing the green panel and Buttons 1-3. I know I am missing something but not sure what it is. Could someone please help me to fixing my code. Thank you for your help.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class TwoPanels extends Frame
    {
    	private static JButton btn1;
    	private static JButton btn2;
    	private static JButton btn3;
    	private static JButton btn4;
    	private static JButton btn5;
    	private static JButton btn6;
    	private static JFrame f;
    
    
    	public static void main(String[] args)
    	{
    
    		JFrame f = new JFrame ("Frame with FlowLayout");
    
    		JPanel p1 = new JPanel (new FlowLayout());
    		JButton btn1=new JButton("Button 1");
    		JButton btn2=new JButton ("Button 2");
    		JButton btn3=new JButton ("Button 3");
    
    
    		JPanel p2 = new JPanel (new FlowLayout());
    		JButton btn4=new JButton("Button 4");
    		JButton btn5=new JButton ("Button 5");
    		JButton btn6=new JButton ("Button 6");
    
    		p1.add(btn1);
    		p1.add(btn2);
    		p1.add(btn3);
    
    		p2.add(btn4);
    		p2.add(btn5);
    		p2.add(btn6);
    
    		f.setSize(600,300);
    		f.setLocation (400,200);
    		f.getContentPane().add(p1);
    		f.getContentPane().add(p2);
    
    		p1.setBackground(Color.GREEN);
    		p2.setBackground(Color.RED);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close frame upon
       		f.setVisible(true);
     	 }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The JFrame uses BorderLayout by default, and since you never change the JFrame's layout, it remains BorderLayout. When adding components to this JFrame without specifying a BorderLayout constant, they will be added by default into the BorderLayout.CENTER position, and the last component added this way will cover all other components added.

    Better to make the JFrame use a different layout such as a GridLayout(1, 0) or a BoxLayout, or whatever.

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

    Default

    When adding components to this JFrame without specifying a BorderLayout constant, they will be added by default into the BorderLayout.CENTER position, and the last component added this way will cover all other components added.
    OT nitpick: actually, the last component added is at the bottom, not the top, of the stacking order. In most cases, any previously added component isn't seen because it has its default size of [0, 0].

    Play with this ;)
    Java Code:
    import java.awt.Color;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.*;
    
    public class BorderLayoutOverlay {
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          public void run() {
            new BorderLayoutOverlay().makeUI();
          }
        });
      }
    
      public void makeUI() {
        MouseAdapter listener = new MouseAdapter() {
    
          Point p = null;
    
          @Override
          public void mousePressed(MouseEvent e) {
            p = e.getLocationOnScreen();
          }
    
          @Override
          public void mouseDragged(MouseEvent e) {
            JComponent c = (JComponent) e.getSource();
            Point l = c.getLocation();
            Point here = e.getLocationOnScreen();
            c.setLocation(l.x + here.x - p.x, l.y + here.y - p.y);
            p = here;
          }
        };
    
        JLabel label1 = new JLabel("Drag me!");
        label1.setOpaque(true);
        label1.setBounds(250, 250, 100, 100);
        label1.setBackground(Color.CYAN);
        label1.addMouseListener(listener);
        label1.addMouseMotionListener(listener);
    
        JLabel label2 = new JLabel("I'm at the back!");
    
        JFrame frame = new JFrame();
        frame.add(label1);
        frame.add(label2);
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }
    db

  4. #4
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    I'm stuck using the FlowLayout manager for this program. Basically instructions are:
    1. Create a frame and set its layout to FlowLayout
    2. Create 2 panels and add them to the frame
    3. Each panel contains three buttons. The panel uses FlowLayout

    Then once I have completed this set, do it again but this time with BorderLayout & GridLayout.

  5. #5
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    This is my simple frame
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Frame
    {
    
    	private static JFrame frame;
    
    
    
      	public static void main(String[] args)
    	{
    
        JFrame frame = new JFrame ("Frame");
        frame.setLayout(new FlowLayout());
        frame.setSize(450,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close frame u
        frame.setVisible(true);
          }
    
    }

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    I don't see the part where you create the panels and the buttons in your latest code.
    Also (very important) look at DB's example above to see the structure that you should use for your program.

  7. #7
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    This is probably a code mess and not perfect in the code layout. I am new at Java and trying to figure this out and receiving different information (google) is confusing.


    1. Create a frame and set its layout to FlowLayout
    2. Create 2 panels and add them to the frame
    3. Each panel contains three buttons. The panel uses FlowLayout


    Java Code:
    //frame with FLowLayout without any content
    
    
    import javax.swing.*;
    import java.awt.*;
    
    public class Frame
    {
    
    	private static JFrame frame;
      	
      	public static void main(String[] args)
    	{
    
        JFrame frame = new JFrame ("Frame");
        frame.setLayout(new FlowLayout());
        frame.setSize(450,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close frame u
        frame.setVisible(true);
      	}
    
    }

    [code]

    //code for 2 panels and 3 buttons
    //this is where the p1 with buttons 1-3 and green background is not visible when i compile and run.


    javax.swing.*;
    import java.awt.*;

    public class TwoPanels extends Frame
    {
    private static JButton btn1;
    private static JButton btn2;
    private static JButton btn3;
    private static JButton btn4;
    private static JButton btn5;
    private static JButton btn6;

    public static void main(String[] args)
    {

    super("Frame");
    JPanel p1 = new JPanel (new FlowLayout());
    JButton btn1=new JButton("Button 1");
    JButton btn2=new JButton ("Button 2");
    JButton btn3=new JButton ("Button 3");
    p1.add(btn1);
    p1.add(btn2);
    p1.add(btn3);
    p1.setBackground(Color.GREEN);

    JPanel p2 = new JPanel (new FlowLayout());
    JButton btn4=new JButton("Button 4");
    JButton btn5=new JButton ("Button 5");
    JButton btn6=new JButton ("Button 6");
    p2.add(btn4);
    p2.add(btn5);
    p2.add(btn6);
    p2.setBackground(Color.RED);



    f.getContentPane().add(p1);
    f.getContentPane().add(p2);
    f.setSize(765,690);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close frame upon
    f.setVisible(true);
    }
    }

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by collwill View Post
    This is probably a code mess and not perfect in the code layout. ...
    Yeah, that's why I said above that

    "Also (very important) look at DB's example above to see the structure that you should use for your program."

    You see all those static variables you have everywhere are not very good. Darryl.Burke has already posted an example (complete with a main method) which you can use as a template for your swing programs so that they are structured correctly.

  9. #9
    narmadha is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Arrow Re: Frame with 2 Panels

    Try working with this code.. u ll get 2 pannels in a frame with 3 buttons on each..
    You can mention the layout for ur frame as this [ f.setLayout(new GridLayout());].. hope u got wt u wanted..


    import javax.swing.*;
    import java.awt.*;

    public class TwoPanels extends Frame
    {
    /**
    *
    */
    private static final long serialVersionUID = 1L;
    public static void main(String[] args)
    {

    JFrame f = new JFrame ("Frame with FlowLayout");

    //JFrame q = new JFrame ("Frame with FlowLayout");

    JPanel p2 = new JPanel (new FlowLayout());
    JButton btn4=new JButton("Button 4");
    JButton btn5=new JButton ("Button 5");
    JButton btn6=new JButton ("Button 6");

    JPanel p1 = new JPanel (new FlowLayout());
    JButton btn1=new JButton("Button 1");
    JButton btn2=new JButton ("Button 2");
    JButton btn3=new JButton ("Button 3");

    p1.add(btn1);
    p1.add(btn2);
    p1.add(btn3);

    p2.add(btn4);
    p2.add(btn5);
    p2.add(btn6);

    f.setLayout(new GridLayout());
    f.setSize(600,300);
    f.setLocation (400,200);
    f.getContentPane().add(p1);

    f.setSize(600,300);
    f.setLocation (400,200);
    f.getContentPane().add(p2);


    p1.setBackground(Color.GREEN);
    p2.setBackground(Color.RED);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close frame upon
    f.setVisible(true);
    //q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close frame upon
    // q.setVisible(true);

    }
    }

  10. #10
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Frame with 2 Panels

    @narmadha, please note the date on the original post - I would presume the original poster has moved on after almost two years. Further, note this is a technical forum - using abbreviations such as 'ur' and 'wt' is discourage and often not only makes your post harder to understand but can also lead to confusion. Lastly, I recommend reading the following:
    The Problem with Spoon-feeding
    I am locking this thread.

Similar Threads

  1. Need some help with panels inside panels
    By kakefjes in forum AWT / Swing
    Replies: 0
    Last Post: 03-17-2011, 11:36 AM
  2. Replies: 4
    Last Post: 01-18-2011, 08:22 AM
  3. Java slave Frame access to its owner main frame problem
    By cagdaseckin in forum New To Java
    Replies: 0
    Last Post: 12-10-2010, 10:40 AM
  4. how to add 2 panels in a panel inside a frame
    By clydedoris in forum New To Java
    Replies: 4
    Last Post: 07-31-2010, 09:11 AM
  5. Replies: 7
    Last Post: 07-09-2008, 12:06 AM

Posting Permissions

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