280 likes | 375 Views
GUI Applications. Contents. Labels, Text Fields, and Buttons Problem: The Hello Application Development Steps Option Panes New Requirement for the Hello Application: Using an Option Pane New Requirement: Removing the Button and Allowing the User to Press Enter Exercises
E N D
Contents • Labels, Text Fields, and Buttons • Problem: The Hello Application • Development Steps • Option Panes • New Requirement for the Hello Application: Using an Option Pane • New Requirement: Removing the Button and Allowing the User to Press Enter • Exercises • Converting Kilometers to Miles • The Number Guessing Game
1. Problem: The Hello Application • Develop a GUI application that asks the user to input a name and then displays a welcome message to that name
2. Development Steps • Creating the Project • Creating the View 2.1. Creating the GUI Container 2.2. Putting GUI Components into the GUI Container • Implementing an Event Handler for the Button • Running the project
1. Creating the Project • File > New Project… • New Project • Categories: Java • Projects: Java Application • Next • New Java Application • Project Name: HelloDesktop • Turn off “Create Main Class” • Finish
2. Creating the View2.1. Creating the GUI Container • Right-click the project name > New > JFrame Form… • New JFrame Form • Class Name: HelloView • Package: hello • Finish • Naming the Form • title: hello
Label • text: Name: • Text Field • text: Enteryourname • Variable Name: nameTextField • Button • text: Say Hello • Variable Name: sayHelloButton • Label • text: • Variable Name: messageLabel
3. Implementing an Event Handler for the Button • Double-click the SayHello button (or right-click > Events > Action > actionPerformed)
4. Running Application • Click the RunMainProject(F6) button
1. New Requirement for the Hello Application • Modify the application so that after the user enters a name, the welcome message will be shown in a different window
2. New Requirement: Removing the Button and Allowing the User to Press Enter • Modify the application so that after the user enters a name and press the Enter key, the welcome message will be shown in a different window
Solution • Removing the Button • Implementing an Event Handler for the Text Field • Double-click the text field
1. Exercise: Converting Kilometers to Miles • A kilometer is equal to 0.6214 miles. Develop a GUI in which the user will enter a distance in kilometers, and then click a button to see the distance converted to miles
2. Exercise: The Number Guessing Game • Develop an application that asks the user to input a guess number (See the next slide for an example). After the user enters a number and clicks the Submit button, the application displays one among the following messages • Sorry, try something higher • Too bad, go lower • Sweet, you got it right!
Development Steps • Creating the View • Generating the target number • Adding functionality to the button 3.1. Validating the input 3.2. Matching the guess number to the target
3. Adding Functionality to Button3.2. Matching the Guess Number to the Target
References • GUI Building in NetBeans IDE 5.5. http://testwww.netbeans.org/kb/55/quickstart-gui.html • Developing a Web Application. http://testwww.netbeans.org/kb/55/vwp-helloweb.html • Adding Functionality to Buttons: A Beginner Guide. http://testwww.netbeans.org/kb/articles/gui-functionality.html • Starting Out With Java 5 from control structures to objects. Tony Gaddis • Introduction to Facelets. http://testwww.netbeans.org/kb/55/quick-start-facelets-in-netbeans.html