The Final Form of the AtmGUI class


The Final Form of the AtmGUI class

Here is the final form of AtmGUI class that we developed in this chapter. You can see that the code really isn't that long, nor is it all that complicated.

 import javax.swing.*; import java.awt.*; import javax.swing.border.BevelBorder; import java.awt.event.*; public class AtmGUI extends JFrame                 implements ActionListener {   private JComboBox comboBox;   private JTextField textField;   private JButton runButton;   private JTextArea textArea;   private JLabel comboLabel, textFieldLabel;   private JLabel textAreaLabel;   private USatm76 atm;   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));     runButton.addActionListener(this);     //  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));     //  The components are added to a JPanel before     //  being added to the content pane to maintain     //  their preferred sizes.     JPanel northPanel = new JPanel();     northPanel.add(comboLabel);     northPanel.add(comboBox);     northPanel.add(new JLabel("     "));     northPanel.add(textFieldLabel);     northPanel.add(textField);     northPanel.add(new JLabel("     "));     northPanel.add(runButton);     JPanel centerPanel = new JPanel();     centerPanel.add(textAreaLabel);     centerPanel.add(textArea);     //  Add the JPanel objects to the content pane     getContentPane().setLayout(new BorderLayout());     getContentPane().add(                      northPanel, BorderLayout.NORTH);     getContentPane().add(                      centerPanel, BorderLayout.CENTER);     //  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);   }   //  The actionPerformed() method is called when   //  the "Compute" button is pressed. A USatm76   //  object is created and the atmospheric conditions   //  written to the JTextArea.   public void actionPerformed(ActionEvent event) {     String units = (String)comboBox.getSelectedItem();     double altitude =                Double.parseDouble(textField.getText());     atm = new USatm76(units, altitude);     String label[] = atm.getLabels();     textArea.setText("");     textArea.append("geometric altitude    = "+                      atm.getAltitude()+label[0]);     textArea.append("\ngeopotential altitude = "+            atm.getGeoPotentialAltitude()+label[1]);     textArea.append("\ntemperature           = "+                      atm.getTemperature()+label[2]);     textArea.append("\npressure              = "+                      atm.getPressure()+label[3]);     textArea.append("\nmolar mass            = "+                      atm.getMolarMass()+label[4]);     textArea.append("\ndensity               = "+                      atm.getDensity()+label[5]);   }   public static void main(String args[]) {     AtmGUI gui = new AtmGUI();   } } 

When you compile and run the AtmGUI program, Figure 26.1 is a typical screen shot of what you will see.



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