?? messagebox.java
字號:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MessageBox extends Dialog
{
protected Button okButton;
protected Button cancelButton;
protected static boolean back;
final static public int OKCANCEL = 1;
final static public int OKONLY = 0;
protected static Frame createdFrame;
public MessageBox(Frame parent,String message,String title ,int type)
{
super(parent,true);
this.setResizable(false);
this.setTitle(title);
this.setBackground(Color.lightGray);
Panel msgboxPanel = new Panel();
Panel buttonPanel = new Panel();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
okButton = new Button(" OK ");
cancelButton = new Button(" Cancel ");
Label messagelabel = new Label(message,Label.CENTER);
BListener bListener = new BListener();
okButton.addActionListener(bListener);
cancelButton.addActionListener(bListener);
this.addWindowListener(new WListener());
msgboxPanel.setLayout(gridbag);
gbc.anchor = GridBagConstraints.CENTER ;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.ipady = 5;
gbc.insets = new Insets(10,20,5,20);
gbc.gridy = 0;
gbc.gridx = GridBagConstraints.REMAINDER;
msgboxPanel.add(messagelabel,gbc);
gbc.insets = new Insets(0,20,20,20);
gbc.weighty = 0;
gbc.weightx = 0;
gbc.gridy = 1;
gbc.ipadx = 10;
gbc.ipady = 5;
msgboxPanel.add(buttonPanel,gbc);
switch(type)
{
case OKCANCEL: // OkCancel
{
buttonPanel.setLayout(new GridLayout(1,2,10,10));
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
break;
}
case OKONLY: // Ok only
{
buttonPanel.setLayout(new BorderLayout());
buttonPanel.add(okButton,"Center");
break;
}
}
add(msgboxPanel);
pack();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension scrSize = toolkit.getScreenSize();
setLocation((scrSize.width - getSize().width)/2,(scrSize.height- getSize().height)/2);
}
public static boolean createMessageBox(String msg)
{
return createMessageBox(null,msg,"",0);
}
public static boolean createMessageBox(String msg,String title)
{
return createMessageBox(null,msg,title,0);
}
public static boolean createMessageBox(String msg,String title,int type)
{
return createMessageBox(null,msg,title,type);
}
public static boolean createMessageBox(Frame parent,String msg,String title,int type)
{
if ( parent == null )
{
if (createdFrame == null )
{
createdFrame = new Frame("Dialog");
createdFrame.setVisible(false);
}
parent = createdFrame;
}
MessageBox msgbox = new MessageBox(parent,msg,title ,type);
msgbox.show();
if(back) return true;
else return false;
}
public class BListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if( e.getSource() == okButton)
{
back = true;
dispose();
}
else
{
back= false;
dispose();
}
}
}
public class WListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
back = false;
dispose();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -