How to add multiple panels in JFrame in Java
In this tutorial, we will learn how can we add multiple panels in JFrame in Java.
Let’s discuss what’s panel is. Panel or JPanel in Java can be defined as an invisible window inside a JFrame where various elements can be added and arranged to make complex GUI applications easily. The general procedure to construct a JPanel and add the various elements to it is:
- Firstly call the JPanel () constructor to create a Panel. We can also add Layout Manager inside the JPanel constructor and give a design to the passed constructor { JPanel(LayoutManager layout) }.
- Then we use Component add() to add the Element inside JPanel. Components can be anything eg. Text Field, Labels, Button, Checkbox, etc;
- In the next step, we use the setLayout(Layout Manager layout) function, it can be BorderLayout, FlowLayout, etc depending on the GUI’s demand. If the setLayout of the JPanel is set to null, we need to set the location and size of the JPanel, but I prefer setLayout null because it allows me to decide where to put my JPanel or any other component in the JFrame.
Lets Code and Create a JFrame window and add a single JPanel
CODE:
import javax. swing.*; import java.awt.*; class panelx extends JFrame{ panelx(){ setTitle("JPANEL CREATION"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null); //setting the bounds for the JFrame setBounds(100,100,500,300); Container c=getContentPane(); //Creating a JPanel for the JFrame JPanel panel=new JPanel(); //setting the panel layout as null panel.setLayout(null); //adding a label element to the panel JLabel label=new JLabel("HELLO I AM SUBHOJEET"); label.setBounds(70,100,200,50); panel.add(label); // changing the background color of the panel to yellow panel.setBackground(Color.yellow); panel.setBounds(100,50,300,200); //adding the panel to the Container of the JFrame c.add(panel); setVisible(true); } public static void main(String[] args) { new panelx(); } }
OUTPUT:
Creating a JFrame Window with Multiple panels added to it
The above output shows a panel yellow in color with a JLabel with “Hello I am Subhojeet” typed into it. This is how a panel is added in the JFrame in Java . Now Let’s do some improvisation by adding multiple panel into the JFrame with different colors into it.
CODE:
import javax.swing.*; import javax.swing.border.Border; import java.awt.*; class panelx extends JFrame{ panelx(){ setTitle("JPANEL CREATION"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null); //setting the bounds for the JFrame setBounds(100,100,645,470); Border br = BorderFactory.createLineBorder(Color.black); Container c=getContentPane(); //Creating a JPanel for the JFrame JPanel panel=new JPanel(); JPanel panel2=new JPanel(); JPanel panel3=new JPanel(); JPanel panel4=new JPanel(); //setting the panel layout as null panel.setLayout(null); panel2.setLayout(null); panel3.setLayout(null); panel4.setLayout(null); //adding a label element to the panel JLabel label=new JLabel("Panel 1"); JLabel label2=new JLabel("Panel 2"); JLabel label3=new JLabel("Panel 3"); JLabel label4=new JLabel("Panel 4"); label.setBounds(120,50,200,50); label2.setBounds(120,50,200,50); label3.setBounds(120,50,200,50); label4.setBounds(120,50,200,50); panel.add(label); panel2.add(label2); panel3.add(label3); panel4.add(label4); // changing the background color of the panel to yellow //Panel 1 panel.setBackground(Color.yellow); panel.setBounds(10,10,300,200); //Panel 2 panel2.setBackground(Color.red); panel2.setBounds(320,10,300,200); //Panel 3 panel3.setBackground(Color.green); panel3.setBounds(10,220,300,200); //Panel 4 panel4.setBackground(Color.cyan); panel4.setBounds(320,220,300,200); // Panel border panel.setBorder(br); panel2.setBorder(br); panel3.setBorder(br); panel4.setBorder(br); //adding the panel to the Container of the JFrame c.add(panel); c.add(panel2); c.add(panel3); c.add(panel4); setVisible(true); } public static void main(String[] args) { new panelx(); } }
OUTPUT:
So you can see that I created 4 Panels and set their location and size according to your own will by using the setBounds() function. You can also use Flowlayout and BorderLayout but I would suggest as a beginner you should start using basic features and learn how the Java GUI application works.
Hope you like the content and is helpful to you.
Happy Coding!!!!!.
Leave a Reply