The windows discussed in previous sections placed components in the content pane of the frame. The content pane is the container where any types of components or small containers are placed.
The largest container defined is an object of class JFrame. The only way to add components to this object is by using its content pane. Smaller containers are objects of class JPanel. These objects can contain components such as labels, buttons, text fields, and other components. With panels (objects of class JPanel), it is possible to organize a GUI in a hierarchical manner. A GUI with several panels is structured in such a manner that all the panels and other components are placed in the content pane of the frame.
The following statements declare two panel objects, create the panels, and set the layout manager for each panel.
object fpanel of class JPanel object bpanel of class JPanel . . . create bpanel of class JPanel create fpanel of class JPanel . . . call setLayout of fpanel using gridmanager call setLayout of bpanel using flowmanager
The various components can be added to each panel, and the panels can be added to the content pane of the frame.
For example, class Psalarygui has two panels that are placed in the content pane of the frame using border layout. The first panel contains the labels and text fields using the grid layout. The second panel contains the two buttons using flow layout. The KJP code with the implementation of class Psalarygui follows.
import all javax.swing // Library for graphics import all java.awt // description This program computes the salary increase for an employee. If his/her salary is greater than $45,000, the salary increase is 4.5%; otherwise, the salary increase is 5%. Two panels are used in this class. The first is used to place the labels and text fields, the second is used to place the two buttons. The program uses the following components: buttons, labels, and text fields. */ class Psalarygui is public description This is the main function of the application. */ function main is constants integer WIDTH = 400 integer HEIGHT = 300 objects object sal_frame of class JFrame object cpane of class Container object fpanel of class JPanel object bpanel of class JPanel object namelabel of class JLabel object salarylabel of class JLabel object agelabel of class JLabel object increaselabel of class JLabel object nametfield of class JTextField object agetfield of class JTextField object inctfield of class JTextField object salarytfield of class JTextField object bordermanager of class BorderLayout object gridmanager of class GridLayout object flowmanager of class FlowLayout object incbutt of class JButton object quitbutt of class JButton object actlistener of class Sal_listener begin create sal_frame of class JFrame using "Salary Problem" set cpane = call getContentPane of sal_frame create bordermanager of class BorderLayout create gridmanager of class GridLayout using 4, 2 create flowmanager of class FlowLayout create bpanel of class JPanel create fpanel of class JPanel create namelabel of class JLabel using "Enter name: " create agelabel of class JLabel using "Enter age: " create salarylabel of class JLabel using "Enter salary: " create increaselabel of class JLabel using "Salary increase: " create nametfield of class JTextField using 20 create agetfield of class JTextField using 20 create salarytfield of class JTextField using 20 create inctfield of class JTextField using 20 create incbutt of class JButton using "Increase" create quitbutt of class JButton using "Quit" call setLayout of fpanel using gridmanager call setBackground of fpanel using Color.lightGray call setLayout of bpanel using flowmanager call setBackground of bpanel using Color.blue call setLayout of cpane using bordermanager call add of fpanel using namelabel call add of fpanel using nametfield call add of fpanel using agelabel call add of fpanel using agetfield call add of fpanel using salarylabel call add of fpanel using salarytfield call add of fpanel using increaselabel call add of fpanel using inctfield call add of bpanel using incbutt call add of bpanel using quitbutt create actlistener of class Sal_listener using salarytfield, inctfield call addActionListener of incbutt using actlistener call addActionListener of quitbutt using actlistener call add of cpane using fpanel, BorderLayout.CENTER call add of cpane using bpanel, BorderLayout.SOUTH call setSize of sal_frame using WIDTH, HEIGHT call setVisible of sal_frame using true endfun main endclass Psalarygui
Class Psalarygui is similar in functionality to class Csalarygui, which is discussed in Section 14.4.5. The listener class, Sal_listener, is the same as used in class Csalarygui.
On the cD | The KJP code with the implementation of class Psalarygui is stored in the file Psalarygui.kpl, and the Java implementation in the file Psalarygui.java. |
When the program that consists of classes Psalarygui and Sal_listener executes, the window shown is similar to the program discussed in Section 14.4.5. Figure 14.10 shows the window for class Psalarygui.
Figure 14.10: A frame with two panels.
The main difference between the two programs is noted in the two buttons located at the bottom of the frame. These buttons are smaller and on a blue background.