Choosing a Container


The first step in developing a GUI is to decide which underlying container will be used to store the other GUI components. A container is a GUI component on which you can place other components . There are a variety of container classes defined in the java.awt and javax.swing packages. Most GUI applications will use a high-level container as their base container. High-level containers are those that have the different layers , or panes, we discussed earlier. The most commonly used high-level container is a JFrame that comes with a title, border, and other platform-specific features such as minimize/maximize, iconify, and close buttons . The atmosphere modeling GUI will use a JFrame as its container.

To create the GUI front-end for the USatm76 program, we will define a class named AtmGUI that is a subclass of JFrame . We will need to import the JFrame class into the program, but just to simplify things later on we will import the entire javax.swing package. We will define an AtmGUI constructor that at this point will simply set the title and size of the frame and make the frame visible. The setDefaultCloseOperation() method ensures that the application will terminate if the window is closed.

We will also build the main() method into the AtmGUI class. When the AtmGUI class is compiled and run, the GUI is launched and appears on your screen. Here is the initial AtmGUI class source code.

 import javax.swing.*; public class AtmGUI extends JFrame {   public AtmGUI() {     //  Add a title to the JFrame, size it, and make it     //  visible.     setTitle("Atmosphere Modeling Tool");     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     setBounds(100,100,500,300);     setVisible(true);   }   public static void main(String args[]) {     AtmGUI gui = new AtmGUI();   } } 

When you compile and run this program, an empty frame will appear on your screen.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net