📄 msgdialog.java
字号:
case ICON_INFORMATION:
dlg_icon.setIcon(icon_information);
break;
case ICON_WARNING:
dlg_icon.setIcon(icon_warning);
break;
case ICON_STOP:
dlg_icon.setIcon(icon_stop);
break;
case ICON_MULTI_CENTER:
case ICON_MULTI_LEFT:
break;
default:
dlg_icon.setIcon(icon_information);
break;
}
dlg_infoPanel.add(dlg_icon);
}
//add the message
if ((icon == ICON_MULTI_CENTER) || (icon == ICON_MULTI_LEFT)) {
Font f = new Font("TimesRoman",Font.BOLD,14);
text = parseStr(msg, ";");
int nLines = text.length;
if (nLines > 1)
dlg_textPanel.setLayout(new GridLayout(nLines, 1, 1, 1));
else
dlg_textPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
for (int i = 0; i<text.length; i++)
dlg_textPanel.add(new Label(text[i]));
dlg_textPanel.setFont(f);
}
else {
//compute the number of lines needed to display the text
//respecting the maxWidth
Font f = parent.getFont();
//if no font is defined yet, use the default
if (f==null)
f = new Font("Serif", Font.PLAIN, 12);
int nLines = (int)Math.ceil((double)Toolkit.getDefaultToolkit().getFontMetrics(f).stringWidth(msg) / (double)getMaxWidth());
if (nLines > 1)
dlg_textPanel.setLayout(new GridLayout(nLines, 1, 1, 1));
else
dlg_textPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
//split the original message in several lines
text = parseMessage(msg, nLines);
//and add the lines
for (int i = 0; i<text.length; i++)
dlg_textPanel.add(new Label(text[i]));
}
dlg_infoPanel.add(dlg_textPanel);
//add the buttons
if ((style & MB_YES) == MB_YES) {
dlg_bYes = new DlgButton(DlgButton.YES);
dlg_bYes.addActionListener(this);
dlg_buttonPanel.add(dlg_bYes);
}
if ((style & MB_NO) == MB_NO) {
dlg_bNo = new DlgButton(DlgButton.NO);
dlg_bNo.addActionListener(this);
dlg_buttonPanel.add(dlg_bNo);
}
if ((style & MB_OK) == MB_OK) {
dlg_bOk = new DlgButton(DlgButton.OK);
dlg_bOk.addActionListener(this);
dlg_buttonPanel.add(dlg_bOk);
}
if ((style & MB_CANCEL) == MB_CANCEL) {
dlg_bCancel = new DlgButton(DlgButton.CANCEL);
dlg_bCancel.addActionListener(this);
dlg_buttonPanel.add(dlg_bCancel);
}
if ((style & MB_RETRY) == MB_RETRY) {
dlg_bRetry = new DlgButton(DlgButton.RETRY);
dlg_bRetry.addActionListener(this);
dlg_buttonPanel.add(dlg_bRetry);
}
//add the two main panels
add(dlg_infoPanel, BorderLayout.CENTER);
add(dlg_buttonPanel, BorderLayout.SOUTH);
//alow the dialog to close
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
close();
}
});
//allow the dialog to close on ESC key
addKeyListener(new KeyAdapter() {
public void KeyPressed(KeyEvent e) {
System.out.println("keypressed");
if (e.getKeyCode()==KeyEvent.VK_ESCAPE) {
close();
}
}
});
//doLayout and center the dialog
pack();
centerWindow(this);
}
/**
* Give the return value of the dialog.
* The return value indicates the button used to exit the dialog
* <p>
* Values can be :
* <ul>
* <li>ID_NONE : the user pressed the ESC key or use the close button</li>
* <li>ID_OK : the user pressed the Ok button</li>
* <li>ID_CANCEL : the user pressed the Cancel button</li>
* <li>ID_YES : the user pressed the Yes button</li>
* <li>ID_NO : the user pressed the No button</li>
* <li>ID_RETRY : the user pressed the Retry button</li>
* </ul>
*/
public int getRetValue() {
return ret_value;
}
private void close() {
setVisible(false);
dispose();
}
public void setBackground(Color c) {
//set the background
dlg_infoPanel.setBackground(c);
dlg_buttonPanel.setBackground(c);
repaint();
}
private void centerWindow(Window win) {
int w, h;
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
w = win.getBounds().width;
h = win.getBounds().height;
win.setBounds((screensize.width-w)/2,(screensize.height-h)/2, w, h);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == dlg_bOk) {
ret_value = ID_OK;
} else
if (ae.getSource() == dlg_bYes) {
ret_value = ID_YES;
} else
if (ae.getSource() == dlg_bNo) {
ret_value = ID_NO;
} else
if (ae.getSource() == dlg_bCancel) {
ret_value = ID_CANCEL;
} else
if (ae.getSource() == dlg_bRetry) {
ret_value = ID_RETRY;
}
if (ret_value != ID_NONE) {
//if the action was triggered by a valid button
close();
}
}
private int getMaxWidth() {
//arbitrary choose a maxwidth of 1/3 of the screen size
return Toolkit.getDefaultToolkit().getScreenSize().width / 3;
}
private String [] parseMessage(String msg, int nLines) {
String l[] = new String[nLines];
int meanLineLength = msg.length() / nLines;
int i, j;
j = 0;
i = 0;
while (j<nLines) {
//find the first space after meanLineLenght
i = msg.indexOf(" ", meanLineLength * (j+1));
if (i==-1)
l[j] = (msg.substring(meanLineLength*j, msg.length()));
else
l[j] = (msg.substring(meanLineLength*j, i));
j++;
}
return l;
}
}
class ImageCanvas extends Canvas {
private Image i;
private Container parent;
private Dimension minSize;
private boolean trueSizeKnown = false;
protected ImageCanvas(Container parent) {
setParent(parent);
minSize = new Dimension(30,30);
}
protected ImageCanvas(Image i, Container parent) {
this(parent);
setIcon(i);
}
protected void setIcon(Image i) {
this.i = i;
int w = i.getWidth(this);
int h = i.getHeight(this);
//wait that the image is loaded
while (w<0 || h<0) {
w = i.getWidth(this);
h = i.getHeight(this);
try { Thread.currentThread().sleep(10); }
catch (InterruptedException e) {}
}
minSize = new Dimension(w, h);
setSize(minSize);
parent.doLayout();
parent.repaint();
}
protected void setIcon(String icon) {
setIcon(Toolkit.getDefaultToolkit().getImage(icon));
}
protected void setParent(Container parent) {
this.parent = parent;
}
public Dimension getPreferredSize() {
return getMinimumSize();
}
public synchronized Dimension getMinimumSize() {
return minSize;
}
public void paint(Graphics g) {
g.drawImage(i, 0, 0, this);
}
}
class DlgButton extends Button {
//default labels
//do not forget to change the constructor if adding one
protected static final String YES = "Yes";
protected static final String NO = "No";
protected static final String OK = "Ok";
protected static final String CANCEL = "Cancel";
protected static final String RETRY = "Retry";
private static Vector labels = null;
private static Dimension minSize = null;
//default constructor
protected DlgButton() {
this("");
}
protected DlgButton(String label) {
super(label);
//the labels (only once)
if (labels == null) {
labels = new Vector();
labels.addElement(YES);
labels.addElement(NO);
labels.addElement(OK);
labels.addElement(CANCEL);
labels.addElement(RETRY);
}
}
public Dimension computeMinSize() {
//compute the minimum size of the buttons
//h = standard height
//w = the width of the largest label
//do this only once
if (minSize == null) {
//determine the minsize of the button
//must be as large as the largest
Font f = getFont();
FontMetrics fm = getFontMetrics(f);
//the maximum width
int maxwidth = 0;
for(Enumeration e = labels.elements(); e.hasMoreElements();) {
String l = (String)e.nextElement();
maxwidth = Math.max(maxwidth, fm.stringWidth(l));
}
int decorationWidth, h;
//keep the normal heigth
h = super.getPreferredSize().height;
//platform independant way of computing
//the size of the "decoration" ie. the space
//between the text and the border of the button
decorationWidth = super.getPreferredSize().width - fm.stringWidth(getLabel());
minSize = new Dimension(maxwidth + decorationWidth, h);
}
return minSize;
}
public Dimension getPreferredSize() {
return getMinimumSize();
}
public synchronized Dimension getMinimumSize() {
return computeMinSize();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -