Selecting the GUI Components


Selecting the GUI Components

Now that we have our container it is time to define the GUI components that will be placed in it. The GUI components will be used to receive the input data, create a USatm76 object, and display the resulting atmospheric conditions inside the GUI. Keep in mind that Java GUI development is another one of those areas where there is more than one way to do things. There is nothing magical about the selection of GUI components used in this example. The key is to evaluate what you want to do, and then decide which components will best allow you to do it.

There are two input arguments to the USatm76 constructor, so we need to identify two GUI components to accept the input data. The first component will accept the system of units under which the results will be displayed. There are only two choices for the system of units, "SI" and "English" . To make it easy on the user and to prevent bad input for this parameter, we will build the two choices into a list-type GUI component called a JComboBox . A JComboBox presents the user with one or more choices. The currently selected item is displayed on the screen. If you click on the JComboBox , the other available choices become visible. We will add two items to the JComboBox . One is the String "SI" and the other the String "English" .

The second input parameter is the altitude at which the atmospheric conditions will be computed. The altitude can theoretically be any nonzero floating point number. To accept the altitude input we will use a JTextField component. This component provides a box containing a single line of text into which the user can type input. The JTextField will be given an initial value of "20000.0" and will have 10 columns with which to input data. The altitude model that we implemented in the USatm76 class is valid for any altitude greater than 0 and less than 86,400 meters. To be rigorous about the process, we could include code that would reject negative altitudes or those above 86,400 meters , but for purposes of code listing simplicity and clarity we will omit that step.

There also has to a component that tells the GUI that the atmospheric conditions should be computed using the currently selected system of units and altitude. The logical choice here is a button component. We will use a JButton that represents a simple, rectangular button. We will give the JButton the label "Compute" and will make some changes to the default style. For one thing, we will give the JButton a bevel border that makes the button appear raised from the screen.

The final GUI component we need is one that will display the atmospheric conditions at the specified altitude using the specified system of units. We will use a JTextArea component that represents a rectangular, multiline text box. The JTextArea is given an initial size of 10 rows and 55 columns. The display font of the JTextArea is changed to Courier so the output will line up nicely .

It's usually a good idea to label your GUI components so the user knows what input is required. Three JLabels are created, one each for the JComboBox , JTextField , and JTextArea . The updated AtmGUI class code listing is shown here. We've added code to create each of the GUI components discussed in the preceding paragraphs.

 import javax.swing.*; import java.awt.*; import javax.swing.border.BevelBorder; public class AtmGUI extends JFrame {   private JComboBox comboBox;   private JTextField textField;   private JButton runButton;   private JTextArea textArea;   private JLabel comboLabel, textFieldLabel;   private JLabel textAreaLabel;   public AtmGUI() {     //  Create a JComboBox to choose between     //  English and SI units.     comboLabel = new JLabel("Units");     comboBox = new JComboBox();     comboBox.addItem("SI");     comboBox.addItem("English");     //  Create a JTextField for the input altitude.     textFieldLabel = new JLabel("Altitude");     textField = new JTextField("20000.0",10);     //  Create a JButton that will compute the     //  atmospheric conditions when pressed. Register an     //  ActionListener for the JButton     runButton = new JButton("Compute");     runButton.setBorder(                   new BevelBorder(BevelBorder.RAISED));     runButton.setPreferredSize(new Dimension(60,35));     //  Create a JTextArea that will display the results     textAreaLabel = new JLabel("Results");     textArea = new JTextArea(10,55);     textArea.setFont(new Font("Courier",Font.PLAIN,12));       //  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 code, you still get the same blank frame on your screen. That is because although we have defined our GUI components, we haven't added the GUI components to one of the panes of the JFrame .



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