import javax.swing.*; public class MyPanel extends JPanel { public MyPanel() { // set panel's layout and add components setLayout(new FlowLayout()); add(new JLabel("This is a JPanel")); add(new JButton("Click me!")); // set panel's visibility setVisible(true); } }
import javax.swing.*; public class MainFrame extends JFrame { public MainFrame() { // create a new panel JPanel panel = new JPanel(); // set panel's layout and add components panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(new JTextField(10)); panel.add(new JButton("Submit")); // add panel to the frame and set frame's properties add(panel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 200); setVisible(true); } }In this example, a new `MainFrame` is created which extends `JFrame`. Inside the constructor of the frame, a new `JPanel` called `panel` is created. Its layout is set to `BoxLayout` and two components are added (a `JTextField` and a `JButton`). The panel is then added to the frame using `add(panel)` method call. Finally, the frame's properties are set and is made visible using `setVisible` method call. Package Library: The `setVisible` method belongs to the java.awt.event package/library.