Download presentation
1
Dialog Boxes
2
JOptionPane Class Allows you to display a dialog box
Small graphical window that displays a message to the user or requests input Types: Message Dialog Input Dialog Need to import javax.swing.JOptionPane
3
Displaying Message Dialogs
showMessaegDialog method – used to display a message dialog JOptionPane.showMessageDialog(null, “Hello World”); First argument will be discussed when we learn about Graphical User Interfaces (GUIs) – used to display other graphical windows Second argument is the message that we wish to display
4
Displaying Input Dialogs
Quick and easy way to enter data String name; name = JOptionPane.showInputDialog(“Enter your name.”);
5
Ending Program Need: System.exit(0);
When using the JOptionPane class, you must end a program Does not automatically stop executing when the end of the main method is reached because the class causes an additional task(thread) to run Need: System.exit(0); Needs an integer argument - value 0 traditionally indicates that the program ended successfully
6
Converting String Input to Numbers
Unlike the Scanner class, the JOptionPane class does not have different methods for reading values of different data types as input showMessaegDialog method always returns the user’s input as a String, even if a numeric value was entered What is the problem with this?
7
Converting String Input to Numbers
Use methods to convert string value to a numeric value Method Example Code Double.parseDouble double num; num = Double.parseDouble(str) Integer.parseInt int num; num = Integer.parseInt(str)
8
Example *Now you could use number to perform math methods* int number;
String str; str = JOptionPane.showInputDialog(“Enter a number.”) number = Integer.parseInt(str); *Now you could use number to perform math methods*
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.