📄 msgdialog.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
/**
* This class mimics the MessageBox API from Windows
* It can be used for notifying short message to the user
* or for debugging purpose
* <p>
* <b>Here is how to use this class :</b><br>
* <code>int ret = MsgDialog.msg( ... valid argument list ...)</code>
* <br>There are many overloaded methods for <code>msg</code>, please refer to the doc below.
* <p>
* The dialog size if arbitrary fixed to maximum 1/3 of screen size
* If the message is longer than the availabe space, it is displayed
* using multiple lines. Lines are cutted at SPACE character only
*
* @since JDK 1.1.X
* @author <a href="mailto:sst@ambrasoft.lu">S閎astien Stormacq</a>
* works as Software Engineer for AmbraSoft sa,<br>
* a company based in Luxembourg, specialized in client server development.<br>
* His main interests are Internet/Intranet technologies and object oriented development.<br>
* Sebastien Stormacq is a Sun Certified Java Programmer.<br>
* You can reach him by email at <a href="mailto:sst@ambrasoft.lu">sst@ambrasoft.lu</a>
* @version 1.0 February 1998
*/
public class MsgDialog extends Dialog implements ActionListener {
/**
* Style constant : displays a single button labelled "Retry"
*/
public static final int MB_RETRY = 2;
/**
* Style constant : displays a single button labelled "No"
*/
public static final int MB_NO = 4;
/**
* Style constant : displays a single button labelled "Yes"
*/
public static final int MB_YES = 8;
/**
* Style constant : displays a single button labelled "Ok"
*/
public static final int MB_OK = 16;
/**
* Style constant : displays a single button labelled "Cancel"
*/
public static final int MB_CANCEL = 32;
/**
* Style constant : displays two buttons labelled "Yes" and "No"
*/
public static final int MB_YESNO = MB_YES | MB_NO;
/**
* Style constant : displays two buttons labelled "Ok" and "Cancel"
*/
public static final int MB_OKCANCEL = MB_OK | MB_CANCEL;
/**
* Style constant : displays three buttons labelled "Ok","Cancel" and "Retry"
*/
public static final int MB_OKCANCELRETRY = MB_OK | MB_CANCEL | MB_RETRY;
private static final int ID_NONE = 0;
/**
* Return value : the user pressed the "Yes" button
*/
public static final int ID_YES = 1;
/**
* Return value : the user pressed the "No" button
*/
public static final int ID_NO = 2;
/**
* Return value : the user pressed the "Cancel" button
*/
public static final int ID_CANCEL = 3;
/**
* Return value : the user pressed the "Ok" button
*/
public static final int ID_OK = 4;
/**
* Return value : the user pressed the "Retry" button
*/
public static final int ID_RETRY = 5;
/**
* Do not display an icon
*/
public static final int ICON_NONE = 0;
/**
* Icon to display : an "Information" sign
*/
public static final int ICON_INFORMATION = 1;
/**
* Icon to display : an "STOP" sign
*/
public static final int ICON_STOP = 2;
/**
* Icon to display : an "Warning" sign
*/
public static final int ICON_WARNING = 3;
public static final int ICON_MULTI_CENTER= 11;
public static final int ICON_MULTI_LEFT = 12;
/**
* This method displays a dialog to the user
*
* @param Frame the parent window of the dialog, this frame will receive
* the focus when the user exits the dialog
* @param String the title of the dialog
* @param String the message to be displayed
* @param int the style of the dialog, ie. one of the MB_XXX constants. These can be added
* @param int the icon to use, see the ICON_XXX constants
* @return int the id of the button pressed by the user
* @see MB_OK
* @see MB_CANCEL
* @see MB_YES
* @see MB_NO
* @see MB_RETRY
* @see ID_OK
* @see ID_YES
* @see ID_NO
* @see ID_CANCEL
* @see ID_RETRY
* @see ICON_STOP
* @see ICON_INFORMATION
* @see ICON_WARNING
*
*/
public static int msg(Frame parent, String title, String msg, int style, int icon) {
MsgDialog d = new MsgDialog(parent, title, msg, style, icon);
d.setVisible(true);
return d.getRetValue();
}
/**
* This method displays a dialog without title.
* It is equivalent to give an empty title as parameter
*
* @param Frame the parent window of the dialog, this frame will receive
* the focus when the user exits the dialog
* @param String the message to be displayed
* @param int the style of the dialog, ie. on eof the MB_XXX constants. These can be added
* @param int the icon to use, see the ICON_XXX constants
* @return int the id of the button pressed by the user
* @see msg(java.awt.Frame, java.lang.String, java.lang.String, int, int)
*/
public static int msg(Frame parent, String msg, int style, int icon) {
return msg(parent, parent.getTitle(), msg, style, icon);
}
/**
* This method displays a dialog without icon.
* It is equivalent to give the NO_ICON parameter
*
* @param Frame the parent window of the dialog, this frame will receive
* the focus when the user exits the dialog
* @param String the title of the dialog
* @param String the message to be displayed
* @param int the style of the dialog, ie. on eof the MB_XXX constants. These can be added
* @return int the id of the button pressed by the user
* @see msg(java.awt.Frame, java.lang.String, java.lang.String, int, int)
*/
public static int msg(Frame parent, String title, String msg, int style) {
return msg(parent, title, msg, style, ICON_NONE);
}
/**
* This method displays a dialog without title and without icon.
* It is equivalent to give an empty title and the NO_ICON parameters
*
* @param Frame the parent window of the dialog, this frame will receive
* the focus when the user exits the dialog
* @param String the message to be displayed
* @param int the style of the dialog, ie. on eof the MB_XXX constants. These can be added
* @return int the id of the button pressed by the user
* @see msg(java.awt.Frame, java.lang.String, java.lang.String, int, int)
*/
public static int msg(Frame parent, String msg, int style) {
return msg(parent, parent.getTitle(), msg, style, ICON_NONE);
}
/**
* This method displays a simple message with no title and no icon.
* It is equivalent to give an empty title and the NO_ICON parameters.
* The default style is MB_OK
*
* @param Frame the parent window of the dialog, this frame will receive
* the focus when the user exits the dialog
* @param String the message to be displayed
* @return int the id of the button pressed by the user
* @see msg(java.awt.Frame, java.lang.String, java.lang.String, int, int)
*/
public static int msg(Frame parent, String msg) {
return msg(parent, parent.getTitle(), msg, MB_OK, ICON_NONE);
}
/**
* This method displays a dialog to the user.
* A default parent frame is created
*
* @param String the title of teh dialog
* @param String the message to be displayed
* @param int the style of the dialog, ie. on eof the MB_XXX constants. These can be added
* @param int the icon to use, see the ICON_XXX constants
* @return int the id of the button pressed by the user
* @see msg(java.awt.Frame, java.lang.String, java.lang.String, int, int)
*/
public static int msg(String title, String msg, int style, int icon) {
Frame parent = new Frame();
int ret = msg(parent, title, msg, style, icon);
parent.dispose();
return ret;
}
/**
* This method displays a dialog without icon.
* It is equivalent to give the NO_ICON parameter.
* A default parent frame is created.
*
* @param String the title of the dialog
* @param String the message to be displayed
* @param int the style of the dialog, ie. on eof the MB_XXX constants. These can be added
* @return int the id of the button pressed by the user
* @see msg(java.awt.Frame, java.lang.String, java.lang.String, int, int)
*/
public static int msg(String title, String msg, int style) {
return msg(title, msg, style, ICON_NONE);
}
/**
* This method displays a dialog without icon and without title.
* It is equivalent to give the NO_ICON parameter and to give an empty title.
* A default parent frame is created
*
* @param String the message to be displayed
* @param int the style of the dialog, ie. on eof the MB_XXX constants. These can be added
* @return int the id of the button pressed by the user
* @see msg(java.awt.Frame, java.lang.String, java.lang.String, int, int)
*/
public static int msg(String msg, int style) {
return msg("", msg, style, ICON_NONE);
}
/**
* This method displays a dialog a simple message.
* A default parent frame is created.
* This dialog has no title and no icon, its style is MB_OK.
* This is the quickest way to display a simple message to the user.
*
* @param String the message to be displayed
* @return int the id of the button pressed by the user
* @see msg(java.awt.Frame, java.lang.String, java.lang.String, int, int)
*/
public static int msg(String msg) {
return msg("", msg, MB_OK, ICON_NONE);
}
private static final String icon_information = "information.gif";
private static final String icon_warning = "warning.gif";
private static final String icon_stop = "stop.gif";
private static final int hGap = 20;
private static final int vGap = 15;
private ImageCanvas dlg_icon;
private Label dlg_msg;
private DlgButton dlg_bOk;
private DlgButton dlg_bCancel;
private DlgButton dlg_bYes;
private DlgButton dlg_bNo;
private DlgButton dlg_bRetry;
//contains the buttons
private Panel dlg_buttonPanel;
//contains the icon and the text
private Panel dlg_infoPanel;
//contains the text
private Panel dlg_textPanel;
//return value of the MsgBox
//default value is used when the user close
//the dlg without using the buttons
private int ret_value = ID_NONE;
//constructors are private : this class can never be instanciated
private MsgDialog(Frame parent, String title, boolean modal) {
super(parent, title, modal);
}
public static String[] parseStr(String stref, String delim) {
String items[];
int numTokens;
int n;
String noitems[] = {""};
if ((stref == null) || ("".equals(stref.trim())))
return noitems;
StringTokenizer st = new StringTokenizer(stref,delim);
numTokens = st.countTokens();
items = new String[numTokens];
n = 0;
while (st.hasMoreTokens())
{
try
{
items[n] = st.nextToken();
n++;
}
catch(Exception e)
{
return noitems;
}
}
if (n == 0)
return noitems;
else
return items;
}
private MsgDialog(Frame parent, String title, String msg, int style, int icon) {
this(parent, title, true);
//does not allow to resize the dialog
setResizable(false);
String [] text;
dlg_infoPanel = new Panel();
dlg_buttonPanel = new Panel();
dlg_textPanel = new Panel();
dlg_icon = new ImageCanvas(dlg_infoPanel);
dlg_infoPanel.setLayout(new FlowLayout(FlowLayout.CENTER, hGap, vGap));
dlg_buttonPanel.setLayout(new FlowLayout());
//add the icon
if (icon != ICON_NONE) {
switch (icon) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -