| We usually start with a brief description of the pupose of the java file (written as comments). | |||||||
| // Press.java |
|||||||
|
// remember that the file name must match the class name exactly // An applet for getting simple text input from a textField by pressing a button // lines starting with // are comments (ignored by the compiler) // |
|||||||
| Next comes the import section, where we load the required, shared java libraries. | |||||||
| import | java.applet.*; | |
|||||
| import | java.awt.*; | |
|||||
| import | java.awt.event.*; | |
|||||
| the import instruction | the Java Class Libraries | (the * means load All the Classes found in the specified library folder) | |||||
The first line of the class definition looks like this |
|||||||
| public | class | Press | extends Applet | implements ActionListener |
|||
| the visibility of the object public or private |
the type of object a class |
the name of the object the class name Press |
creates an instance of an Applet to be viewed by a Browser |
uses the ActionListener class which listens for button clicks |
|||
{ |
|||||||
| TextField | yourName | = new TextField | ("Type your name in here", 25); | ||||
| the predefined AWT class type of the data member | the variable name assigned to the data member | instantiate the new data member ie. allocate the memory storage | pass 2 parameters to the constructor method, the Prompt and the Field width | ||||
| Button | pressMe | = new Button | ("Press Me"); | ||||
| the predefined AWT class type of the data member | the variable name assigned to the data member | instantiate the new data member ie. allocate the memory storage | pass a parameter to the constructor method, the Text on the button | ||||
| Label | theOutput | = new Label | ("This is where the output will appear"); | ||||
| the predefined AWT class type of the data member | the variable name assigned to the data member | instantiate the new data member ie. allocate the memory storage | pass a parameter to the constructor method, the Text in the Label | ||||
| public | void | init | ( ) | ||||
| the visibility of the Method | the return value of the Method | the name assigned to the Method | no parameters are passed | ||||
{ |
|||||||
| add | (yourName); | ||||||
| the predefined Method to put a data member on the page | the data member to be added | ||||||
| add | (pressMe); | ||||||
| the predefined Method to put a data member on the page | the data member to be added | ||||||
| add | (theOutput); | ||||||
| the predefined Method to put a data member on the page | the data member to be added | ||||||
| pressMe | .addActionListener | (this); | |||||
| the data member we are listening to | the predefined addActionListener Method to recieve action events | listen to this button | |||||
} |
|||||||
| public | void | actionPerformed | (ActionEvent | thisEvent) | |||
| the visibility of the Method | the return value of the Method | the name assigned to the Method | the type of the parameter being passed | the variable name assigned to the parameter | |||
{ |
|||||||
| if | (thisEvent | .getSource | == "pressMe") | ||||
| the conditional keyword if | the passed parameter | the predefined Method to retreive the Button name that triggered the event | the name of the Button being compared | ||||
{ |
|||||||
| String | message | = yourName | .getText( ) | ||||
| the predefined LANG class type of the data member | the variable name assigned to the data member | the data member (textField) holding the text data | the predefined Method to retreive the text from the textField | ||||
| theOutput | .setText | ("Hello " + message + ", welcome."); | |||||
| the data member (Label) recieving the text data | the predefined Method to insert text into the Label | the String message to insert into the Label Field | |||||
} |
|||||||
} |
|||||||
} |
|||||||
The Complete Java Code File looks like this |
// Press.java // remember that the file name must match the class name exactly // Getting simple text input // lines starting with // are comments (ignored by the compiler) // import java.applet.*; import java.awt.*; import java.awt.event.*; // classes used by the applet public class Press extends Applet implements ActionListener {
TextField yourName = new TextField("Type your name in here", 25); Button pressMe = new Button("Press Me"); Label theOutput = new Label("This is where the output will appear"); public void init() {
add( yourName ); add( pressMe ); add( theOutput ); // Make the button "listen" for an event (eg a press) pressMe.addActionListener(this); public void actionPerformed(ActionEvent thisEvent) // the button is pressed {
{
String message = yourName.getText(); theOutput.setText("Hello " + message + ", welcome."); |
The Finished Java Applet looks like this |
|
| The applet before any user interaction | After data has been entered and the button pressed |
![]() |
![]() |
The original location of this page was http://www.uwcsea.edu.sg/comp/ib/javasource/SourceCode.html. It was mirrored from the web site of IB Computer Science, United World College of South East Asia, Singapore developed by IB Computer Science Teacher Richard Jones. This page was mirrored from that original site using the utility Webstripper. It was mirrored to ensure availability of it to local students as well as relieving hits on the original site. Aside from this paragraph and the immediately following footer, no other changes have been made.
|
|
![]() csgate@donaldson.org ICQ# 62833374 |
|