I am writing a seprate driver class for my programme, but I need to know how to add two panels to the jframe.

Any one help?


Just want to add some things to think about.
If any items like labels or buttons are going to be added to your two panels (those you want to add to the JFrame) do that first.

Decide how you want to add the two panels by setting preferred layout to your JFrame, if you have not done that yet.

Then add your panels to the JFrame like Ezzaral said.

I can show you a small example where i have put some background colors on the two panels so you can see them when compiling.

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

public class MyFrame extends JFrame
 public static void main(String[] args) 
      JPanel one = new JPanel();

      JPanel two = new JPanel();

      JFrame f = new JFrame();

      //the layout makes the panels stand side by side from left to right
      f.setLayout(new GridLayout(1, 2));
      //adding panels
      //setBounds combines setLocation and setSize
      f.setBounds(100, 100, 600, 400);
      f.setTitle("MY FRAME");

Hope this will help you.
Good luck =)

This article has been dead for over six months. Start a new discussion instead.