📄 messagebox.java
字号:
//********************************************************************
//
// MessageBox.java
//
// MessageBox类 ,通用的MessageBox模式对话框
//
//*********************************************************************
import java.awt.*;
//使用静态函数 createMessageBox(...)弹出一个模式对话框
//createMessageBox()参数:
// String msg ,需要显示的消息
// String title = "" , 对话框的标题,缺省为空串
// int type = 0 ,类型,0 只有OK按钮,1有OK,Cancel两个按钮,缺省为0
public class MessageBox extends Dialog
{
protected Button okButton;
protected Button cancelButton;
protected static boolean back;
protected static Frame createdFrame;
public MessageBox(Frame parent,String message,String title ,int type)
{
super(parent,true);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
okButton = new Button("OK");
cancelButton = new Button("Cancel");
Label messagelabel = new Label(message);
setLayout(gridbag);
this.setResizable(false);
add(messagelabel);
pack();
setLayout(null);
int x = this.getSize().width;
int y = this.getSize().height;
switch(type)
{
case 1:
{
if (x<210) x = 210;
y+=20;
int bx = x/2 -80;
messagelabel.setBounds(20,20,x,y-20);
this.setBounds(200,200,x+20,y+70);
okButton.setBounds(bx,y+10,75,25);
cancelButton.setBounds(bx+80,y+10,75,25);
add(okButton);
add(cancelButton);
back = false;
this.setTitle(title);
break;
}
case 0:
{
if (x<120) x = 120;
y+=10;
int bx = x/2 -30;
this.setBounds(200,200,x,y+70);
okButton.setBounds(bx,y+10,75,25);
add(okButton);
back = false;
this.setTitle(title);
break;
}
}
}
public boolean action (Event evt,Object obj)
{
if (evt.target == okButton )
{
// hide();
back = true;
dispose();
if(createdFrame != null)
{
createdFrame.setVisible(false);
}
}
else
if (evt.target == cancelButton )
{
back = false;
dispose();
if(createdFrame != null)
{
createdFrame.setVisible(false);
}
}
return super.action(evt,obj);
}
public boolean handelEvent(Event e)
{
if (e.id == Event.WINDOW_DESTROY )
{
back = false;
dispose();
}
return super.handleEvent(e);
}
public static boolean createMessageBox(String msg)
{
return createMessageBox(msg,"",0);
}
public static boolean createMessageBox(String msg,String title)
{
return createMessageBox(msg,title,0);
}
public static boolean createMessageBox(String msg,String title,int type)
{
if (createdFrame == null )
{
createdFrame = new Frame("Dialog");
}
MessageBox msgbox = new MessageBox(createdFrame,msg,title ,type);
createdFrame.setSize(msgbox.getSize().width ,msgbox.getSize().height);
msgbox.show();
if(back) return true;
else return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -