• The JPanel

    The JPanel component is also a container holding different components. You can add components to the JPanel and the JPanel to the JFrame. For example:

    package com.javapointers.javase;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /**
     *
     * @author javapointers
     */
    public class JButtonTest implements ActionListener {
    
        JFrame frame;
        JButton button;
        JPanel panel;
    
        public JButtonTest() {
            frame = new JFrame();
            frame.setTitle("My JFrame");
            frame.setSize(500, 400);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            button = new JButton("Click Me!");
            button.addActionListener(this);
    
            panel = new JPanel();
            panel.setLayout(new FlowLayout());
            panel.add(button);
            
            frame.add(panel);
            frame.setVisible(true);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("You have click the button");
        }
    
        public static void main(String args[]) {
            JButtonTest test = new JButtonTest();
        }
    }
    
    
    

    This is the same example when in our JButton tutorial with little modification. In this example, we have created our frame, button and our panel. The button was placed inside the panel, and the panel was placed inside the frame. JPanel works like our JFrame.

    We can also add multiple JPanels inside our JFrame. Try this:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.javapointers.javase;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /**
     *
     * @author javapointers
     */
    public class JButtonTest {
    
        JFrame frame;
        JPanel firstPanel, secondPanel;
    
        public JButtonTest() {
            frame = new JFrame();
            frame.setTitle("My JFrame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            firstPanel = new JPanel();
            firstPanel.setPreferredSize(new Dimension(250, 400));
            firstPanel.setBackground(Color.BLUE);
    
            secondPanel = new JPanel();
            secondPanel.setPreferredSize(new Dimension(250, 400));
            secondPanel.setBackground(Color.RED);
    
            frame.setLayout(new FlowLayout());
            frame.add(firstPanel);
            frame.add(secondPanel);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String args[]) {
            JButtonTest test = new JButtonTest();
        }
    }
    
    

    When we run the program the output will be:

    Explanation:
    We have created two panels and a frame. The two panels have been added to our frame.

    1. firstPanel = new JPanel()

      – create an instance of JPanel.

    2. firstPanel.setPreferredSize(new Dimension(250, 400))

      – set the size of our panels to 250 in width, 400 in height.

    3. firstPanel.setBackground(Color.BLUE);

      – set the background of our panel to color blue.

    4. frame.setLayout(new FlowLayout())

      – set the layout of the frame to flow layout. You will learn more about layout manager later on the next chapter.

    5. frame.add(firstPanel)

      – add our panel to the frame.

    6. frame.pack()

      – sets the size of our frame relative to overall size of whatever components inside the frame.

    Related Post