Presentation is loading. Please wait.

Presentation is loading. Please wait.

JLabel, JTextField, JButton and JOptionPane

Similar presentations


Presentation on theme: "JLabel, JTextField, JButton and JOptionPane"— Presentation transcript:

1 JLabel, JTextField, JButton and JOptionPane
GUI Applications JLabel, JTextField, JButton and JOptionPane

2 Contents Problem: The Hello Application Converting Kilometers to Miles
Problem Description New Requirement: Using an Option Pane New Requirement: Allowing the User to Press Enter Converting Kilometers to Miles The Number Guessing Game

3 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

4 Solution 1.1. Creating the Project 1.2. Developing the View
Creating the GUI Container Putting GUI Components into the GUI Container 1.3. Implementing an Event Handler for the Button 1.4. Running the project 1.5. Summary: The Design

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

6 1.2.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 Properties > title: Hello

7 1.2.2. Putting GUI Components into the Container

8 Label Text Field Button text: Name: text: Enter your name
Variable Name: nameTextField Button text: Say Hello Variable Name: sayHelloButton text: empty Variable Name: messageLabel

9 1.3.3. Implementing an Event Handler for the Button
Double-click the Say Hello button (or right-click > Events > Action > actionPerformed)

10 1.4. Running Application Click the Run Main Project (F6) button

11 1.5. Summary: The Design

12 2. New Requirement: Using an Option Pane
Modify the application so that after the user enters a name, the welcome message will be shown in a different window

13 Solution Delete the messageLabel Modify the event handler:

14 3. New Requirement: 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 also be shown in a different window

15 Solution Develop an Event Handler for the nameTextField
Double-click the text field

16 B. Converting Kilometers to Miles
A kilometer is equal to 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

17 Solution Project name: KmToMiles The Design Developing the View
Developing the Model Developing the Event Handler

18 1. The Design

19 2. Developing the View Frame tile: Kilometers to Miles Conversion
GUI components Label: Enter a distance in kilometers Km text field Convert To Miles button

20 3. Developing the Model

21 4. Developing the Event Handler

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

23

24 Solution Project name: Number Guessing Game The Design
Developing the View Developing the Model Developing the Event Handler

25 1. The Design

26 2. Developing the View Frame title: Number Guessing Game
GUI components Prompt label Guess text field Submit button

27 3. Developing the Model

28 4. Developing the Event Handler
4.1. Declaring the Model Inside the View 4.2. Validating the input 4.3. Matching the guess number to the target

29 4.1. Declaring the Model Inside the View

30 4.2. Validating the Input

31 4.3. Matching the Guess to the Target

32 References GUI Building in NetBeans IDE Developing a Web Application. Adding Functionality to Buttons: A Beginner Guide. Starting Out With Java 5 from control structures to objects. Tony Gaddis Introduction to Facelets.

33 Revision History


Download ppt "JLabel, JTextField, JButton and JOptionPane"

Similar presentations


Ads by Google