import java.awt.*; import javax.swing.*; public class ContainerExample extends JFrame { public ContainerExample() { Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); JButton button1 = new JButton("Button 1"); JButton button2 = new JButton("Button 2"); contentPane.add(button1); contentPane.add(button2); contentPane.validate(); } }
import java.awt.*; import javax.swing.*; public class ContainerExample extends JFrame { public ContainerExample() { Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); contentPane.add(scrollPane, BorderLayout.CENTER); contentPane.validate(); } }In this example, a JFrame is created with a BorderLayout, and a JTextArea is added to the center using a JScrollPane. The validate() method is called to ensure that the text area is properly sized and positioned within the container. Package Library: The Container class is part of the java.awt package, which provides classes for creating graphical user interfaces.