import javax.swing.*; import java.awt.*; public class ExamplePanel extends JPanel { private JLabel label; private JButton button; public ExamplePanel() { // Set JPanel layout to FlowLayout setLayout(new FlowLayout()); // Create a label and add it to panel label = new JLabel("Hello World!"); add(label); // Create a button and add it to panel button = new JButton("Click me!"); add(button); } }
import javax.swing.*; import java.awt.*; public class ExamplePanel extends JPanel { private JButton button1, button2, button3, button4, button5, button6; public ExamplePanel() { // Set JPanel layout to GridLayout setLayout(new GridLayout(2, 3)); // Create buttons and add them to panel button1 = new JButton("Button 1"); add(button1); button2 = new JButton("Button 2"); add(button2); button3 = new JButton("Button 3"); add(button3); button4 = new JButton("Button 4"); add(button4); button5 = new JButton("Button 5"); add(button5); button6 = new JButton("Button 6"); add(button6); } }In this example, we create a new class that extends JPanel and overrides its default constructor. We then set the layout of the panel to GridLayout with 2 rows and 3 columns. We create 6 buttons and add them to the panel. The buttons will be evenly spaced out in the grid. Overall, JPanel is a flexible component that can be used to group other components and organize them in a specific layout. It is part of the javax.swing package library.