/* * File: Converter.java * Author: Java, Java, Java * Description: This version of the Converter class * presents a more elaborate GUI for interacting with * the MetricConverter class. In the previous version * the user had to type in their input. In this version * a numeric key pad is used to input miles. * Note: One shortcoming with the interface for this version * is that it uses a FlowLayout for the keypad. It should * use a GridLayout to arrange the buttons into a two-dimensional * array. */ import javax.swing.*; // Packages used import java.awt.*; import java.awt.event.*; public class Converter extends JFrame implements ActionListener{ // Version 2 private final static int NBUTTONS = 12; private JLabel prompt = new JLabel("Distance in miles: "); private JTextField input = new JTextField(6); private JTextArea display = new JTextArea(10,20); private JButton convert = new JButton("Convert!"); private JPanel keypadPanel = new JPanel(); private JButton keyPad[]; // An array of buttons private String label[] = // An array of button labels { "1","2","3", "4","5","6", "7","8","9", "C","0","." }; /** * Converter() constructor sets the layout and adds * components to the top-level JFrame. Note that components * are added to the ContentPane rather to the JFrame itself. */ public Converter () { getContentPane().setLayout( new FlowLayout() ); initKeyPad(); getContentPane().add(prompt); getContentPane().add(input); getContentPane().add(convert); getContentPane().add(display); getContentPane().add(keypadPanel); display.setLineWrap( true ); display.setEditable( false ); convert.addActionListener(this); input.addActionListener(this); } // Converter() /** * initKeyPad() creates an array of JButtons and organizes them * into a graphical key pad panel. Note that you must distinguish * the JButton array, an internal memory structure, from the * key pad panel, a graphical object that appears in the user interface. */ public void initKeyPad() { keyPad = new JButton[NBUTTONS]; // Create the array itself for(int k = 0; k < keyPad.length; k++) { // For each button keyPad[k] = new JButton(label[k]); // Create a labeled button keyPad[k].addActionListener(this); // and a listener keypadPanel.add(keyPad[k]); // and add it to the panel } // for } // initKeyPad() /** * actionPerformed() handles all action events for the program. * It must distinguish whether the user clicked on what of the * buttons on the keypad or on the "Convert" or "Input" button. * @param e -- the ActionEvent which prompted this method call */ public void actionPerformed(ActionEvent e) { if (e.getSource() == convert || e.getSource() == input) { double miles = Double.valueOf(input.getText()).doubleValue(); double km = MetricConverter.milesToKm(miles); display.append(miles + " miles equals " + km + " kilometers\n"); input.setText(""); } else { // A keypad button was pressed JButton b = (JButton)e.getSource(); if (b.getText().equals("C")) input.setText(""); else input.setText( input.getText() + b.getText() ); } } // actionPerformed() /** * main() creates an instance of this (Converter) class and sets * the size and visibility of its JFrame. * An anonymous class is used to create an instance of the * WindowListener class, which handles the window close events * for the application. */ public static void main(String args[]) { Converter f = new Converter(); f.setSize(400, 300); f.setVisible(true); f.addWindowListener(new WindowAdapter() { // Quit the application public void windowClosing(WindowEvent e) { System.exit(0); } }); } // main() } // Converter ------------------------------------------------------------------------------------------------------- /* * File: MetricConverter.java * Author: Java, Java, Java * Description: This class implements the static milesToKm() * method, which converts miles to kilometers. */ public class MetricConverter { /** * mileToKm() converts miles to kilometers * @param miles -- the number of miles to convert * @return -- a double giving the number of kilometers */ public static double milesToKm(double miles) { return miles / 0.62; } }