Chapter 28. Writing a Simple JFC Program

Getting started using the Swing classes is pretty simple. Application windows inherit from JFrame, and applets inherit from JApplet. The only difference between Frame and JFrame is that you cannot add components or set the layout directly for JFrame. Instead, you must use the getContentPane method to obtain the container in which you can add components and vary the layout.

getContentPane().setLayout(new BorderLayout());
JButton b = new JButton ("Hi");
getContentPane().add(b);       //add a button to the layout

This is sometimes a bit tedious to type each time, so we recommend retrieving the JPanel from the content pane and then adding all the components to that panel.

 JPanel jp = getContentPane() //get the ...

Get Java™ Design Patterns: A Tutorial now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.