O'Reilly logo

Java™ Design Patterns: A Tutorial by James W. Cooper

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required