0

I am trying to design an interface with 4 different panels in 1 single frame. I have included 2 of the panel descriptions in the code I included below.

Below is part of my code:

public class finalFrame extends JFrame {
       PanelA a = new PanelA()
       PanelB b = new PanelB()
       // ...
      public finalFrame() {
          super();
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          //...
          //...
          //...
          add(a);
          add(b);
          } 
}
class PanelA extends JPanel {
      JButton bt = new JButton();
      add(bt);
     //...
  }

class PanelB extends JPanel {
// ...
//... }

class Program {
      public static void main(String [] args) {
            finalFrame fr = new finalFrame();
      }
 }

This code doesn't seem to work (it only displays the last panel at an odd size (Not what I wanted)). Yet, when I set up the different panels WITHIN the frame class (and not individual panel classes), it works perfectly. Why can't I use different panel classes and then just add them all to the final Frame class afterwards?

Furthermore (sorry for all the questions), if I include the panel set up within the frame class and include a frame layout, then it works (as I just mentioned). However, if I include the panel set up within the frame class but I DON'T include a frame layout, then it only displays the last panel using the frame layout. Why isn't it portraying any of the other panels??

Thank you!!

Randall Cook
  • 6,728
  • 6
  • 33
  • 68
vmck
  • 21
  • 3

2 Answers2

0

By default, Frames in Java have BorderLayout as its LayoutManager.

So to add any panel to Frame, you can do like following:

frame.add(panel, BorderLayout.NORTH);
frame.add(panel, BorderLayout.WEST);
frame.add(panel, BorderLayout.SOUTH);
frame.add(panel, BorderLayout.EAST);
frame.add(panel, BorderLayout.CENTER);

These values work and are valid. If you use 4 different locations from above for your four different panels while adding them to your frame, then your panels will be visible.

Here's a demonstration to add four panels using GridLayout:

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GridFrame extends JFrame
{
  private static final long serialVersionUID = 1L;

  public GridFrame()
  {
    super("Grid Demo");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new GridLayout(2, 2));

    add(new PanelA());
    add(new PanelB());
    add(new PanelC());
    add(new PanelD());

    setSize(500, 500);
    setLocationRelativeTo(null);
    setVisible(true);
  }

  public static void main(String[] args)
  {
    // Call Swing stuff like this, since Swing is not thread safe.
    SwingUtilities.invokeLater(new Runnable()
    {
      @Override
      public void run()
      {
        new GridFrame();
      }
    });
  }
}

class PanelA extends JPanel
{
  private static final long serialVersionUID = 1L;

  public PanelA()
  {
    setBackground(Color.RED);
  }
}

class PanelB extends JPanel
{
  private static final long serialVersionUID = 1L;

  public PanelB()
  {
    setBackground(Color.BLUE);
  }
}

class PanelC extends JPanel
{
  private static final long serialVersionUID = 1L;

  public PanelC()
  {
    setBackground(Color.YELLOW);
  }
}

class PanelD extends JPanel
{
  private static final long serialVersionUID = 1L;

  public PanelD()
  {
    setBackground(Color.GREEN);
  }
}
Aman Agnihotri
  • 2,973
  • 1
  • 18
  • 22
  • By Default ther border of `JFrame` is `FlowLayout` . isn't it? – Sarz Feb 27 '14 at 18:11
  • 1
    Border of `JFrame` as `FlowLayout`? By default `JFrame`'s `LayoutManager` is `BorderLayout`. The documentation says: _The default content pane will have a BorderLayout manager set on it._ and since `add()` method in `JFrame` now directly adds `components` to its `content pane`, it works. Its not `FlowLayout`. – Aman Agnihotri Feb 27 '14 at 18:14
  • why is it only showing the last panel that I add though? Does this have to do with the default frame layout? – vmck Feb 27 '14 at 19:02
  • If its your code that you're talking about, then yes. When you do `add(a);`, the frame adds the component `a` in `center`, then when you do `add(b);`, the frame adds the component in center again, effectively covering the previously added components. Use Layout hints as I showed above in my code, and you can then easily resolve the issue. – Aman Agnihotri Feb 27 '14 at 19:18
  • @vmck: You can check out the demo code in my updated answer for further insights. :) – Aman Agnihotri Feb 27 '14 at 19:30
  • @AwfullyAwesome wow that clears so much up thanks! I thought the default layout for frames was GridLayout. But thank you! :) – vmck Feb 27 '14 at 21:02
0

You have to first make your Frame That contain at least four JPanel in your case. Use GridLayout explore some, than make each Panel individually and add in JFrame something like this:

frame.setLayout(new GridLayout(2,2));
frame.add(new Panel2().getPanel());
frame.add(new Panel2().getPanel());
frame.add(new Panel3().getPanel());
frame.add(new Panel4().getPanel());
frame.setSize(100,100);
frame.setVisible(true);
Sarz
  • 1,970
  • 4
  • 23
  • 43
  • No need to do `getPanel()` if `Panel2`, `Panel3` and so on are `JPanel`s. – Aman Agnihotri Feb 27 '14 at 18:16
  • is `new Panel2()` is constructor means no return type, `getPanel()` is the method which return the `JPanel`.. . are we cool? @AwfullyAwesome – Sarz Feb 27 '14 at 18:20
  • Well I was saying in the context of constructors only. If `Panel2()` is the `constructor`, you need not add the `getPanel()` method. Check it out on an IDE to see, if you don't believe me. :) – Aman Agnihotri Feb 27 '14 at 18:24
  • `Container.add(String,Component) is not applicable argument mismatch` for `this.add( new Panel(),BorderLayout.NORTH);` where no error for `this.add( new Panel().getPanel(),BorderLayout.NORTH);` Please can you show any example – Sarz Feb 27 '14 at 18:36
  • 1
    I get it that is why i also Vote up for you, and in older version of JDK 4 or 5 JFrame Default Layout was FlowLayout studied back in college i remember that :):) – Sarz Feb 27 '14 at 20:04
  • Wow.. :D The earliest of JDK's I started with was version 1.4. I started my Swing stuff from JDK 6 I guess, so don't know if it was FlowLayout for JFrame. JPanels have FlowLayout as default still though. :) – Aman Agnihotri Feb 27 '14 at 20:21