JOption Pane Java has many prewritten tools for

  • Slides: 8
Download presentation
JOption. Pane ©Java has many pre-written tools for you to use. ©JOption. Pane is

JOption. Pane ©Java has many pre-written tools for you to use. ©JOption. Pane is a great tool for input/output ©Before you can use a pre-written tool, you must import the files ©JOption. Pane is located in the javax. swing library.

JOption. Pane Continued ©Use either: ©import javax. swing. JOption. Pane; or ©import javax. swing.

JOption. Pane Continued ©Use either: ©import javax. swing. JOption. Pane; or ©import javax. swing. *; ©import statements must come before the class declaration ©I prefer after the comments however.

show. Message. Dialog ©Use this to output to the user. ©JOption. Pane. show. Message.

show. Message. Dialog ©Use this to output to the user. ©JOption. Pane. show. Message. Dialog(null, “How are you? ”); ©The part before the () is like ©System. out. print You must have this null operator!!

show. Input. Dialog ©Input is tricky because it needs a destination. ©For this we

show. Input. Dialog ©Input is tricky because it needs a destination. ©For this we will use a variable. ©Specifically you want a String variable. ©You have had a little experience with String. ©(String[] args)

Variable Detour ©There a few rules with variables. ©First rule is you have to

Variable Detour ©There a few rules with variables. ©First rule is you have to declare them before you can use them. ©Declarations are: type name; ©For instance: String user. Input; ©Then you must assign it: ©user. Input = “pizza”;

show. Input. Dialog Continued ©This command is an assignment. ©String user. Input; user. Input

show. Input. Dialog Continued ©This command is an assignment. ©String user. Input; user. Input = JOption. Pane. show. Input. Dialog Notice there is (“Enter your name”); no null here ©If the user enters “Fred”, the variable named user. Input will have a value of “Fred”.

Outputting Variables ©If I declare: String name; ©Then I assign: name = “Dexter”; ©What

Outputting Variables ©If I declare: String name; ©Then I assign: name = “Dexter”; ©What is the difference between, JOption. Pane. show. Message. Dialog(null, “Dexter”); and JOption. Pane. show. Message. Dialog(null, name); ?

String Concatenation © What if you wanted to say “Hi, Dexter” ? ©…Message. Dialog(null,

String Concatenation © What if you wanted to say “Hi, Dexter” ? ©…Message. Dialog(null, “Hi, ” + name); This concatenates the literal String and the variable value ©Let’s see it all in action!