Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java Notes

Example - Kilometers to Miles - User Interface Only

KmToMi user interface

This program produces a user interface, but there is no listener for the button so it doesn't do anything yet.

  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 
// intro-window/KmToMilesNoModel.java - Input field, button, output field
//    This is the user interface for the Km to Mi conversion.
//    The button does nothing in this program.
//    Fred Swartz, 2004-10-28

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

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


///////////////////////////////////////////////////// class KmToMilesGUI
class KmToMilesGUI extends JFrame {
    //======================================= instance variables //Note 1
    private JTextField m_milesTf      = new JTextField(10);      //Note 2
    private JTextField m_kilometersTf = new JTextField(10);
    private JButton    m_convertBtn   = new JButton("Convert");  //Note 3


    //====================================================== constructor
    public KmToMilesGUI() {
        //... Create content panel, set layout, add components
        JPanel content = new JPanel();                           //Note 4
        content.setLayout(new FlowLayout());   // use FlowLayout //Note 5
        content.add(new JLabel("Kilometers")); // create, add label //Note 6
        content.add(m_kilometersTf);           // add input field
        content.add(m_convertBtn);             // add button
        content.add(new JLabel("Miles"));      // create, add label
        content.add(m_milesTf);                // add output field
        this.setContentPane(content);                            //Note 7
        this.pack();                                             //Note 8
        
        //... Set window characteristics
        this.setTitle("Kilometers to Miles");
    }//end constructor
}//end class KmToMilesGUI

Notes



Note 1: Instance variables are created when an object is created (instanciated). They can be refereced by any of the constructors or methods in a class. They should be declared private. This program uses the convention of begining instance variable names with "m_". This practice is highly recommended for clarity in reading the code.

Note 2: This declares and initializes a text field to be approximately 10 characters wide.

Note 3: This declares and initializes a button with the text "Convert" showing on it. There is no "listener" attached to this button yet, so it doesn't do anything. That will be added in the next example.

Note 4: Components must be placed on the content pane in the JFrame. There are two ways to do this: (1) Get the existing content pane and work with that, or (2) Create a new content pane (usually a JPanel) and then set the content pane of the JFrame to that. The second approach is used here.

Note 5: A layout manager should be explicitly set for the content pane.

Note 6: Because labels are usually not referred to again, there is no need to put them into a named variable.

Note 7: The content pane of this JFrame is now set to the JPanel that we created earlier, named "content".

Note 8: After the content pane of the JFrame has been set and populated with components, it's necessary to call pack() to do the layout -- that is to set the location and size of each of the components.

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 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  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.