0

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?

3
Contributors
2
Replies
5
Views
7 Years
Discussion Span
Last Post by sneaker
0

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 =)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.