Introduction To Java GUI
Introduction To Java GUI
STEP ONE
Create the Project.
i. Choose File > New Project. In the Categories pane, select the Java. In the
Projects pane, choose Java Application. Click Next.
ii. Type Addition in the Project Name field
iii. Uncheck the Create Main Class checkbox.
Click Finish.
The IDE creates the AdditionUI.java
1.
Renaming the Components
i. .
ii. Double-click jLabel1 and change the text property to First Number:.
iii. Double-click jLabel2 and change the text to Second Number:.
iv. Double-click jLabel3 and change the text to Result:.
v. Delete the sample text from jTextField1. You can make the display text editable by
right-clicking the text field and choosing Edit Text from the popup menu. You may have
to resize the jTextField1 to its original size.
Repeat this step for jTextField2 and jTextField3.
vi. Rename the display text of jButton1 to Clear. (You can edit a button's text by right-
clicking the button and choosing Edit Text. Or you can click the button, pause, and then
click again.)
vii. Rename the display text of jButton2 to Add.
viii. Rename the display text of jButton3 to Exit.
Your Finished GUI should now look like the following screenshot:
i. Add functionality to exit button
Right click the Exit button. From the pop-up menu choose Events > Action >
actionPerformed. The IDE will open up the Source Code window and scroll to where
you implement the action you want the button to do when the button is pressed. Enter
the line of code System.exit(0);. As shown below
Right click the Clear button select Events > Action > actionPerformed. Control is transferred to
code window. Add the following lines of codes as shown below
// TODO add your handling code here:
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
iii. Adding functionality to add button
Right click the Add button select Events > Action > actionPerformed. Control is transferred to
code window. Add the following lines of codes as shown below
To run the addition project, Right click on AdditionUI.java in addition project and click
on run file. The GUI application will be up and running as follows:
Enter 22 in the textfield for first number
Enter 30 in the textfield for second number
Click on the add button and the result is 52
Assignment
Modify the user interface for addition to implement the following user interface for arithmetic
operations
ARITHMETIC OPERATION
FIRST NMBER
SECOND NUMBER
RESULT