Java Notes
Example - Generic Calc
This program converts miles into kilometers. It provides a basic plan for making a program that, when a button is pressed, takes input from a field, computes a new value based on that, and puts the result in another field. Extending this program is the basis for many other examples. To customize it:
- Change the documentation.
- Change the names of the components (window, fields, button, etc). Remember to save the file in a name that matches the new class name.
- Add additional labels, fields, or buttons that are needed.
- The button listener may have to be changed to convert the input to the correct type to match the parameter(s) of the method that does the calculations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
// File : GUI/components/calculator/GenericCalc.java
// Purpose: Demo input field, button, output field.
// Author : Fred Swartz, 2006-12-25, Placed in public domain.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
////////////////////////////////////////////////////// class GenericCalc
class GenericCalc extends JFrame {
//======================================================== constants
private static final double KILOMETERS_PER_MILE = 1.609344;
//=============================================== instance variables
private JTextField m_milesTf = new JTextField(10);
private JTextField m_kilometersTf = new JTextField(10);
private JButton m_convertBtn = new JButton("Convert");
//====================================================== method main
public static void main(String[] args) {
JFrame window = new GenericCalc();
}
//====================================================== constructor
public GenericCalc() {
//... Add a listener to the button
m_convertBtn.addActionListener(new ConvertListener());
//... Create content panel, set layout, add components
JPanel content = new JPanel();
content.setLayout(new FlowLayout()); // Use FlowLayout
content.add(new JLabel("Miles")); // Create and add label
content.add(m_milesTf); // Add input field
content.add(m_convertBtn); // Add button
content.add(new JLabel("Kilometers")); // Create and add label
content.add(m_kilometersTf); // Add output field
//... Set window characteristics
setContentPane(content);
pack();
setLocationRelativeTo(null);
setTitle("Miles to Kilometers");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
///////////////////////////////////////// inner class ConvertListener
class ConvertListener implements ActionListener {
//=============================================== actionPerformed
public void actionPerformed(ActionEvent e) {
String miles = m_milesTf.getText(); // Get input
double mi = Double.parseDouble(miles); // Str to double
double km = convertMilesToKilometers(mi); // Miles to kms
m_kilometersTf.setText("" + km); // Set output
}
}
//========================================= convertMilesToKilometers
// Performing the pure logic of the program (often called the model)
// is typically done in a separate class.
// Here we only put it into a separate method.
// You might think this is too trivial to put into a method.
// Perhaps in this case, but it is a very good habit to
// acquire because in "real" programs there will be
// a big payoff in clarity.
public double convertMilesToKilometers(double miles) {
return miles * KILOMETERS_PER_MILE;
}
}
|