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?

Recommended Answers

All 2 Replies

Hi

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();
      one.setBackground(Color.CYAN.darker());

      JPanel two = new JPanel();
      two.setBackground(Color.YELLOW);

      JFrame f = new JFrame();
      f.getContentPane(); 

      //the layout makes the panels stand side by side from left to right
      f.setLayout(new GridLayout(1, 2));
     
      //adding panels
      f.add(one);
      f.add(two);
     
      //setBounds combines setLocation and setSize
      f.setBounds(100, 100, 600, 400);
     
      f.setTitle("MY FRAME");
      f.setResizable(false);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
      f.setVisible(true);
    }
}

Hope this will help you.
Good luck =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.