1 / 28

GUI Applications

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

Download Presentation

GUI Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GUI Applications

  2. 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

  3. A. Labels, Text Fields, and Buttons

  4. The Hello Application

  5. 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

  6. 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

  7. 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

  8. 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

  9. 2.2. Putting GUI Components into the Container

  10. Label • text: Name: • Text Field • text: Enteryourname • Variable Name: nameTextField • Button • text: Say Hello • Variable Name: sayHelloButton • Label • text: • Variable Name: messageLabel

  11. 3. Implementing an Event Handler for the Button • Double-click the SayHello button (or right-click > Events > Action > actionPerformed)

  12. 4. Running Application • Click the RunMainProject(F6) button

  13. B. Option Panes

  14. 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

  15. Solution

  16. 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

  17. Solution • Removing the Button • Implementing an Event Handler for the Text Field • Double-click the text field

  18. C. Exercises

  19. 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

  20. Solution

  21. 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!

  22. 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

  23. 1. Creating the View

  24. 2. Generating the Target Number

  25. 3. Adding Functionality to Button3.1. Validating the Input

  26. 3. Adding Functionality to Button3.2. Matching the Guess Number to the Target

  27. 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

More Related