Regular application and applets can use dialog boxes to make the
experience more graphical. Through the Swing library (import javax.swing.*),
we can use these methods. Applets do not need to import.
The two most useful dialog boxes are:
- showInputDialog - which will prmpt the user for input
- showMessageDialog - which displays a message
To display a message box:
We would say:
Then to call an input dialog (and store what the person types as
inputString)we would say:
your line here");
To get a color you could say:
Color selectedColor = JColorChooser.showDialog(this, "Choose Color", getBackground());
There are many more dialgo boxes A
link of JOptionPane methods
HWJ11 Assignments: (do 3, advanced 4)
- Create an applet ClickMe that has a button and when they click, a message dialog box appear
that says why did you click me? When the user clicks the applet.
- Create an applet EasiestGameEver, where you will have one rectangle
(painted on the screen) on the screen. And if the user clicks it, she is given one message
(you won), if she clicks outside the rectangle she is made fun
- Create an application, not applet (Start in BlueJ with a class called ScareUser and create one method public void scare() - delete all the other code in the class) [import javax.swing.*]. that will using showInputDialog (above) and ask the user for their name
then spit out your "John, Your computer has been successfully
- Create an applet RectColorDraw that will draw rectangles using the mouse and let the user choose the color (using dialog box above).
- Make Color selectedColorbe a global
- Have a button that when the click, the color selector will pop open
- Use that color to draw rectangles.
- For real advanced, it will erase the screen, so use this info.