The class to make dialog boxes is class option pane and it is in javax.swing.*;
Here are some examples:
//Show a warning dialog with the options OK, CANCEL, title 'Warning',
// and message 'Click OK to continue':
Object [] options = {"OK", "CANCEL"};
JOptionPane.showOptionDialog (null, "Click OK to continue", "Warning",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options [0]);
|
|
//Show an error dialog that displays the message, 'alert':
JOptionPane.showMessageDialog (null, "alert", "alert",
JOptionPane.ERROR_MESSAGE);
|
|
//Get the user's name
String input = JOptionPane.showInputDialog ("Please enter
your name"); |
|
//Show a dialog asking the user to select from a combobox:
String [] possibleValues = {"First", "Second", "Third"};
String selectedValue = (String) JOptionPane.showInputDialog (null,
"Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues [0]); |
|
//Show an information panel with the options yes/no and message
// 'choose one':
input2 = JOptionPane.showConfirmDialog (null, "choose one", |
Note also that you can get input from optionpanes. For example, the following code could appear in an actionPerformed method:
//Show an information panel with the options yes/no and message 'choose one': String input2 = JOptionPane.showConfirmDialog (null, "choose one", "choose one", JOptionPane.YES_NO_OPTION);
//Show a dialog asking the user to select from a combobox:
String [] possibleValues = {"First", "Second", "Third"};
String selectedValue = (String) JOptionPane.showInputDialog (null,
"Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues [0]);
showStatus ("Yes or No Option: " + input2 + " Combo Box Option:" + selectedValue);
Note that optionpanes always return strings. In cases where a number is returned, you must use Integer.parseInt() to get the int out of the string.
JOptionPane.showMessageDialog (null, "* * * I N S T R U C T I O N S * * * \n \n" + "The purpose of this very exciting game is to \n" + " guess whether the coin is a head or a tail. \n" + " when it is next flipped. \n" + "If you guess correctly, you get a point. \n \n" + "You can reset at any time by clicking CLEAR.\n \n" + "Have fun!", "Instructions", JOptionPane.QUESTION_MESSAGE);
JOptionPane.showMessageDialog (null, "Hi!", "GREETINGS", JOptionPane.QUESTION_MESSAGE);
Change the part highlighted to have one of the following graphics:
| QUESTION_MESSAGE | |
| WARNING_MESSAGE | |
| ERROR_MESSAGE | |
| INFORMATION_MESSAGE |
The Java Doc for Option Panes: http://java.sun.com/j2se/1.3/docs/api/javax/swing/JOptionPane.html
import java.applet.*; import java.awt.*; import javax.swing.*;
public class cat extends Applet
{
public void init ()
{
JOptionPane.showMessageDialog (null, createImageIcon ("lion.gif"), "A Lion", JOptionPane.INFORMATION_MESSAGE);
}
/**
Makesanimageiconto go on a button
@parampathisavalid path to a jpg or gif image
@returnanImageIcon,ornull if the path was invalid.
Directlyfrom:http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/index.html#RadioButtonDemo
*/
protected static ImageIcon createImageIcon (String path)
{ //change the red to your class name
java.net.URL imgURL = cat.class.getResource (path);
if (imgURL != null)
{
return new ImageIcon (imgURL);
}
else
{
System.err.println ("Couldn't find file: " + path);
return null;
}
}
}