×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: ActionScript
Posted by: David Atkinson
Added: Jan 31, 2016 6:32 AM
Views: 7
Tags: no tags
  1. /**
  2.  * David Atkinson
  3.  * IS6050
  4.  * EX01: Create a GUI that calculates miles per gallon used
  5.  * based on miles driven and gallons used
  6.  */
  7.  
  8. package mpg;
  9.  
  10. import javax.swing.*; //Needed for JOptionPane
  11. import java.awt.event.*;
  12. import java.text.DecimalFormat; //Needed for decimal format
  13.        
  14.  
  15. public class MPGWindow extends JFrame
  16. //class needed to hold inputs, messages to user, window height/width
  17. {
  18.     private JPanel panel;
  19.     private JLabel mpgCalc;
  20.     private JLabel milesTrav;
  21.     private JLabel galUsed;
  22.     private JTextField mileTextField;
  23.     private JTextField galTextField;
  24.     private JButton calcButton;
  25.     private JButton quitButton;
  26.     private final int WINDOW_WIDTH = 255;
  27.     private final int WINDOW_HEIGHT = 200;
  28.    
  29.     public MPGWindow() //Constructor
  30.     {
  31.         setTitle("MPG Calculator");
  32.         setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  33.         setLocationRelativeTo(null);
  34.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.         buildPanel();
  36.         add(panel);
  37.         setVisible(true);
  38.                  
  39.     }
  40.    
  41.     private void buildPanel() //Adds label, text fields, buttons
  42.     {
  43.         mpgCalc = new JLabel();
  44.         milesTrav = new JLabel("Miles Travelled:");
  45.         galUsed = new JLabel("Gallons Used:");
  46.         mileTextField = new JTextField(6);
  47.         galTextField = new JTextField(6);
  48.         calcButton = new JButton("Calculate MPG");
  49.         quitButton = new JButton("Quit");
  50.         calcButton.addActionListener(new CalcButtonListener());
  51.         quitButton.addActionListener(new QuitButtonListener());
  52.         panel = new JPanel();
  53.         panel.add(mpgCalc);
  54.         panel.add(milesTrav);
  55.         panel.add(mileTextField);
  56.         panel.add(galUsed);
  57.         panel.add(galTextField);
  58.         panel.add(calcButton);
  59.         panel.add(quitButton);
  60.             }
  61.    
  62.     private class CalcButtonListener implements ActionListener
  63.     //Handles button clicks
  64.     {
  65.         public void actionPerformed(ActionEvent e)
  66.         {
  67.             double miles;
  68.             double gallons;
  69.             double mpg;
  70.                 miles = Double.parseDouble(mileTextField.getText());
  71.                 gallons = Double.parseDouble(galTextField.getText());
  72.                 mpg = miles/gallons;
  73.                 DecimalFormat formatter = new DecimalFormat("#00.00");
  74.                 JOptionPane.showMessageDialog(null, "Distance: " + formatter.format(miles) +
  75.                         "\n Miles per Gallon: " + formatter.format(mpg));
  76.             }
  77.     }
  78.    
  79.     private class QuitButtonListener implements ActionListener {
  80.         public void actionPerformed(ActionEvent e){
  81.             System.exit(0);
  82.                 }
  83.     }
  84. }