Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java Programming 1 Introduction to Basic GUI Lesson 4 1 Outline What is GUI? Display Text in Dialog box • JOptionPane • Step-by-step explanation Various Message Dialog Icons Summary 2 Graphic User Interface (GUI) Graphic User Interface (GUI) • Pronounced GOO-ee • Program 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. 3 Swing GUI components • Package javax.swing • Components originate from AWT (package java.awt) • Contain look and feel Appearance and how users interact with program • Lightweight components Written completely in Java 4 What is Packages in Java? Packages : • Set of predefined classes for us to use • Groups of related classes called packages • Group of all packages known as Java class library or Java Applications Programming Interface (Java API) 5 Displaying Text in a Dialog Box JOptionPane is in the javax.swing package Class JOptionPane allows us to use dialog boxes. 6 Text in the dialog box Displaying Text in a Dialog Box using JOptionPane 7 Displaying Text in a Dialog Box using JOptionPane : Explanation Line 7: import declarations • Used by compiler to identify and locate classes used in Java programs • Tells the compiler to load class JOptionPane from javax.swing package Javax is the extension package from Java API • Those that begin with java is the Java API core package 8 Displaying Text in a Dialog Box using JOptionPane : Explanation Line 14 : Call the showMessageDialog method from JOptionPane class • This method requires two arguments/ parameters • Multiple arguments separated by commas (,) • For now, first argument always null • Second argument is String ( words/ phrases) • 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 (;) 9 Displaying Text in a Dialog Box using JOptionPane : Explanation Line 17: System.exit(0); //exit the program method exit of class System • Terminates application • Use in any application displaying GUI • Identifiers start with capital letters usually class names Argument of 0 (zero) means application ended successfully • Non-zero usually means an error occurred Class System part of package java.lang • No import declaration is needed java.lang is automatically import in every Java 10 Another GUI Example This example uses input dialogs to input two values from user • Use message dialog to display sum of the two values This is an interactive program • Allow user to "provide" data to program and process them. 11 Another GUI Example: Addition.java 12 Another GUI Example (continue…) 13 The Addition.java Program Output 1. Input 10, press “OK” 2. Input 20, press “OK” 3. The result will show in the message dialog box 14 Another GUI Example: Addition.java (Explanation) Line 16 and 17: are declarations statement • strNum1 and strNum2 are variables. • Variables are : Location in memory that stores a value All variables has to be declared with name and type before use. ( week 2 ) • strNum1 and strNum2 are of type String 15 Another GUI Example: Addition.java (Explanation) 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 16 Another GUI Example: Addition.java (Explanation) Operator s Associativit y Type = Right to left assignment 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. 17 Another GUI Example: Addition.java (Explanation) 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. 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) 18 Another GUI Example: Addition.java (Explanation) 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. 19 Another GUI Example: Addition.java (Explanation) Line 34 and 35: Use showMessageDialog to display results "The sum is " + sum • Uses the operator + to "add" the string literal "The sum is" and sum + operator means, • Concatenation ( append) of a string and another type (int, string etc ..) will results in a new string. • For example: 20 Another GUI Example: Addition.java (Explanation) Different showMessageDialog Compared to Welcome1 class example, here we have used different version of showMessageDialog • Previously, Welcome1class’ showMessageDialog only have two arguments 1 2 Here in Line 34-35, it requires four arguments (instead of two as before) First argument: null for now Second: string to display Third: string in title bar Fourth: type of message dialog with icon 21 Line 35: JOptionPane.PLAIN_MESSAGE means no icon shown Various Message Dialog Icons Figure 2.12: JOptionPane constants for message dialogs 22 Part 2: Java Assignment Statements and Expressions Operator Meaning + Unary plus - no change/positive value - Unary minus - negation/negative value ( example : - 10 ) + Binary plus - addition - 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 ( example : + 5 ) ( example : 5 + 8 ) 23 Java Assignment Statements and Expressions Unary Operator : Represent the value, either negative or positive 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 preincrement) Example : a ++ , ++ a; Unary decrement : an operation which will decrease by 1 to the variable. (post-decrement and predecrement) 24 Example : b -- , -- b ; Unary increment/ Unary decrement 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; 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 25 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. • Example : 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. 26 More Example on ++ and - What are the answers for the pre and post-increment Post-increment 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 Pre-increment int a = 10, b= 12, c = 0; c = (++a) + b; c = 11 + 12 c will have the value 23 a will increment by 1 before the addition to 11 27 Summary Basic Java GUI using JOptionPane Two examples are given here • Welcome1.java • Addition.java Various Message Dialog icons used in JOptionPane 28