Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Generic Calc 
 

This program converts miles into kilometers.

 

Java Notes

Example - Generic Calc

GenericCalc window

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 
 71 
 72 
 73 
 74 
 75 
// GenericCalc.java - Input field, button, output field generic program.
// Fred Swartz, 2004-10-25

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

////////////////////////////////////////////////////// class GenericCalc
class GenericCalc {
    //====================================================== method main
    public static void main(String[] args) {
        JFrame window = new GenericCalcGUI();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }
}


/////////////////////////////////////////////////// class GenericCalcGUI
class GenericCalcGUI extends JFrame {
    //=============================================== instance variables
    private JTextField m_milesTf      = new JTextField(10);
    private JTextField m_kilometersTf = new JTextField(10);
    private JButton    m_convertBtn   = new JButton("Convert");


    //====================================================== constructor
    public GenericCalcGUI() {
        //... 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
        this.setContentPane(content);
        this.pack();
        this.setTitle("Miles to Kilometers");
        this.setResizable(false);
    }//end constructor
    
    
    ///////////////////////////////////////// inner class ConvertListener
    class ConvertListener implements ActionListener {
        //============================================= actionPerformed 
        public void actionPerformed(ActionEvent e) {
            // This only copies input field to output field.
            String in = m_milesTf.getText(); // get input
            double mi = Double.parseDouble(miles);
            double km = convertMilesToKilometers(mi);
            m_kilometersTf.setText("" + km);   // set output
        }
    }//end class ConvertListener
    
    
    //=========================================== 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 mi * 1.62;
    }
    
}//endclass GenericCalcGUI

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.