import java.awt.*; import javax.swing.*; public class ContainerSizeDemo extends JFrame { public ContainerSizeDemo() { Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); JButton button1 = new JButton("Button 1"); contentPane.add(button1); JButton button2 = new JButton("Button 2"); contentPane.add(button2); Dimension size = contentPane.getSize(); System.out.println("Container size: " + size.width + " x " + size.height); setTitle("Container Size Demo"); setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new ContainerSizeDemo(); } }
import java.awt.*; import javax.swing.*; public class ContainerSizeDemo extends JPanel { public ContainerSizeDemo() { setLayout(new BorderLayout()); JLabel label = new JLabel("Hello, World!"); add(label, BorderLayout.CENTER); Dimension size = getSize(); System.out.println("Panel size: " + size.width + " x " + size.height); } public static void main(String[] args) { JFrame frame = new JFrame("Container Size Demo"); frame.setContentPane(new ContainerSizeDemo()); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } }This example creates a `JPanel` component, sets its layout to `BorderLayout`, adds a `JLabel` component to it, and then calls the `getSize()` method on the panel to get its size. The size is then printed to the console. The package library is `javax.swing`.