Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Java Programming 1 Introduction to Basic GUI Lesson 4.

Similar presentations


Presentation on theme: "1 Java Programming 1 Introduction to Basic GUI Lesson 4."— Presentation transcript:

1 1 Java Programming 1 Introduction to Basic GUI Lesson 4

2 2 Outline What is GUI? What is GUI? Display Text in Dialog box Display Text in Dialog box JOptionPaneJOptionPane Step-by-step explanationStep-by-step explanation Various Message Dialog Icons Various Message Dialog Icons Summary Summary

3 3 Graphic User Interface (GUI) Graphic User Interface (GUI) Graphic User Interface (GUI) Pronounced GOO-eePronounced GOO-ee Program interface that takes advantage of the computer’s graphics capabilities to make the program easier to useProgram interface that takes advantage of the computer’s graphics capabilities to make the program easier to use Most Java applications use windows or a dialog box to display the output/input. So far we have used command window via JCreator. Most Java applications use windows or a dialog box to display the output/input. So far we have used command window via JCreator.

4 4 Swing GUI components Swing GUI components Package javax.swingPackage javax.swing Components originate from AWT (package java.awt )Components originate from AWT (package java.awt ) Contain look and feelContain look and feel Appearance and how users interact with program Appearance and how users interact with program Lightweight componentsLightweight components Written completely in Java Written completely in Java

5 5 What is Packages in Java? Packages : Packages : Set of predefined classes for us to useSet of predefined classes for us to use Groups of related classes called packagesGroups of related classes called packages Group of all packages known as Java class library or Java Applications Programming Interface (Java API)Group of all packages known as Java class library or Java Applications Programming Interface (Java API)

6 6 Displaying Text in a Dialog Box JOptionPane is in the javax.swing package JOptionPane is in the javax.swing package Class JOptionPane allows us to use dialog boxes. Class JOptionPane allows us to use dialog boxes. Text in the dialog box

7 7 Displaying Text in a Dialog Box using JOptionPane

8 8 Displaying Text in a Dialog Box using JOptionPane : Explanation Line 7: import declarations Line 7: import declarations Used by compiler to identify and locate classes used in Java programsUsed by compiler to identify and locate classes used in Java programs Tells the compiler to load class JOptionPane from javax.swing packageTells the compiler to load class JOptionPane from javax.swing package Javax is the extension package from Java API Javax is the extension package from Java API Those that begin with java is the Java API core packageThose that begin with java is the Java API core package

9 9 Displaying Text in a Dialog Box using JOptionPane : Explanation Line 14 : Call the showMessageDialog method from JOptionPane class Line 14 : Call the showMessageDialog method from JOptionPane class This method requires two arguments/ parametersThis method requires two arguments/ parameters Multiple arguments separated by commas (, )Multiple arguments separated by commas (, ) For now, first argument always nullFor now, first argument always null Second argument is String ( words/ phrases)Second argument is String ( words/ phrases) Must use double quote to enclose the words you want to display on the dialog box. Must use double quote to enclose the words you want to display on the dialog box. As usual, all the statement must end with semicolon ( ; )As usual, all the statement must end with semicolon ( ; )

10 10 Displaying Text in a Dialog Box using JOptionPane : Explanation Line 17: System.exit(0); //exit the program Line 17: System.exit(0); //exit the program method exit of class System method exit of class System Terminates applicationTerminates application Use in any application displaying GUIUse in any application displaying GUI Identifiers start with capital letters usually class namesIdentifiers start with capital letters usually class names Argument of 0 (zero) means application ended successfully Argument of 0 (zero) means application ended successfully Non-zero usually means an error occurredNon-zero usually means an error occurred Class System part of package java.lang Class System part of package java.lang No import declaration is neededNo import declaration is needed java.lang is automatically import in every Java program java.lang is automatically import in every Java program

11 11 Another GUI Example This example uses input dialogs to input two values from user This example uses input dialogs to input two values from user Use message dialog to display sum of the two valuesUse message dialog to display sum of the two values This is an interactive program This is an interactive program Allow user to "provide" data to program and process them.Allow user to "provide" data to program and process them.

12 12 Another GUI Example: Addition.java

13 13 Another GUI Example (continue…)

14 14 The Addition.java Program Output 1. 2. 3. Input 10, press “OK” Input 20, press “OK” The result will show in the message dialog box

15 15 Another GUI Example: Addition.java (Explanation) Line 16 and 17: are declarations statement Line 16 and 17: are declarations statement strNum1 and strNum2 are variables.strNum1 and strNum2 are variables. Variables are :Variables are : Location in memory that stores a value Location in memory that stores a value All variables has to be declared with name and type before use. ( week 2 ) All variables has to be declared with name and type before use. ( week 2 ) strNum1 and strNum2 are of type StringstrNum1 and strNum2 are of type String

16 16 Another GUI Example: Addition.java (Explanation) Line 19 – 21: are statements which declares variables num1, num2, and sum of data type int Line 19 – 21: are statements which declares variables num1, num2, and sum of data type int You also can declare all the same data type in a single line : For Example: int num1, num2, sum; // declaration

17 17 Another GUI Example: Addition.java (Explanation) Line 24: reads in the first input from the user, representing the first number to be added. Line 24: reads in the first input from the user, representing the first number to be added. Line 25: reads in the second input from the user, representing the second number to be added. Line 25: reads in the second input from the user, representing the second number to be added. Operator s Associativit y Type= Right to left assignment

18 18 Another GUI Example: Addition.java (Explanation) Please take note that the input from the user to the showInputDialog is always a String type. Please take note that the input from the user to the showInputDialog is always a String type. We have to parse ( to change/ convert ) the String type to the appropriate type. We have to parse ( to change/ convert ) the String type to the appropriate type. Line 28 and 29: the method Integer.parseInt to Line 28 and 29: the method Integer.parseInt to Convert String argument into integer (with data type int ) Integer returned by Integer.parseInt is assigned to variable num1 Remember that num1 was declared as type int (it means num1 can hold only integral value)

19 19 Another GUI Example: Addition.java (Explanation) Line 32: Line 32: Calculates the sum of num1 and num2 (from right to left). Uses assignment operator, =, to assign result to variable sum. Read as: sum gets the value of num1 + num2. sum has the type int.

20 20 Another GUI Example: Addition.java (Explanation) Line 34 and 35: Use showMessageDialog to display results Line 34 and 35: Use showMessageDialog to display results "The sum is " + sum "The sum is " + sum Uses the operator + to "add" the string literal "The sum is" and sumUses the operator + to "add" the string literal "The sum is" and sum + operator means, + operator means, Concatenation ( append) of a string and another type (int, string etc..) will results in a new string.Concatenation ( append) of a string and another type (int, string etc..) will results in a new string. For example:For example:

21 21 Different showMessageDialog Compared to Welcome1 class example, here we have used different version of showMessageDialog Compared to Welcome1 class example, here we have used different version of showMessageDialog Previously, Welcome1class’ showMessageDialog only have two argumentsPreviously, Welcome1class’ showMessageDialog only have two arguments Here in Line 34-35, it requires four arguments (instead of two as before) First argument: null for now First argument: null for now Second: string to display Second: string to display Third: string in title bar Third: string in title bar Fourth: type of message dialog with icon Fourth: type of message dialog with icon Line 35: JOptionPane.PLAIN_MESSAGE means no icon shown Line 35: JOptionPane.PLAIN_MESSAGE means no icon shown 1 2 Another GUI Example: Addition.java (Explanation)

22 22 Various Message Dialog Icons Figure 2.12: JOptionPane constants for message dialogs

23 23 Part 2: Java Assignment Statements and Expressions OperatorMeaning + Unary plus - no change/positive value ( example : + 5 ) - Unary minus - negation/negative value ( example : - 10 ) + Binary plus - addition ( example : 5 + 8 ) - Binary minus - subtraction ( example : 8 - 4 ) *Multiplication /Division % Modulus - remainder from integer division ++ Unary increment - add one to variable -- Unary decrement - subtract one from variable

24 24 Java Assignment Statements and Expressions  Unary Operator : Represent the value, either negative or positive Example : - y, + 5 ; Example : - y, + 5 ;  Binary Operator : Can perform arithmetic calculation. Example : a – b, c / d, n % 5, x + y;  Unary increment : an operation which will increase by 1 to the variable. (post-increment and pre- increment) Example : a ++, ++ a;  Unary decrement : an operation which will decrease by 1 to the variable. (post-decrement and pre- decrement) Example : b --, -- b ;

25 25 Unary increment/ Unary decrement The following statement may look strange! The following statement may look strange! int count ; // declaration count = count + 1 ; But, when we remember that the operator = stands for assignment and not for equality, it makes sense; But, when we remember that the operator = stands for assignment and not for equality, it makes sense; it is our instruction to the computer to increment the current value of count by 1 and store to the new result into count’s memory location it is our instruction to the computer to increment the current value of count by 1 and store to the new result into count’s memory location

26 26 Unary increment/ Unary decrement The operators ++ and - -, (the unary increment and unary decrement operators ) are used in Java to increase or decrease the variable by one. The operators ++ and - -, (the unary increment and unary decrement operators ) are used in Java to increase or decrease the variable by one. Example : count = count + 1; can be written byExample : count = count + 1; can be written by 1. count++; // post-increment unary operator OR 2. ++count; // pre-increment unary operator Exercise : Write an expression count = count – 1 by using pre and post decrement unary operator. Exercise : Write an expression count = count – 1 by using pre and post decrement unary operator.

27 27 More Example on ++ and - - What are the answers for the pre and post-increment What are the answers for the pre and post-increment Post-incrementPre-increment int a = 10, b = 12, c= 0; c = (a++) + b; int a = 10, b = 12, c= 0; c = (a++) + b; int a = 10, b= 12, c = 0; c = (++a) + b; int a = 10, b= 12, c = 0; c = (++a) + b; c = 10 + 12 c will have the value 22 a will increment by 1 after the addition to 11 and kept in the memory c = 11 + 12 c will have the value 23 a will increment by 1 before the addition to 11

28 28Summary Basic Java GUI using JOptionPane Basic Java GUI using JOptionPane Two examples are given here Two examples are given here Welcome1.javaWelcome1.java Addition.javaAddition.java Various Message Dialog icons used in JOptionPane Various Message Dialog icons used in JOptionPane


Download ppt "1 Java Programming 1 Introduction to Basic GUI Lesson 4."

Similar presentations


Ads by Google