?? basedialog.java
字號:
/*
$Author: $
$Date: $
$Revision: $
$NoKeywords: $
*/
package jp.co.ntl.awt;
import java.awt.*;
import java.awt.event.*;
//import java.util.*;
import jp.co.ntl.Resource;
public class BaseDialog extends Dialog implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
/** Dialog Type */
public static final int DEFAULT_OPTION = -1;
public static final int NOTHING_OPTION = 0;
public static final int YES_NO_OPTION = 1;
public static final int YES_NO_CANCEL_OPTION = 2;
public static final int OK_CANCEL_OPTION = 3;
/** Return value */
public static final int YES_OPTION = -2;
public static final int NO_OPTION = -3;
public static final int CANCEL_OPTION = -4;
public static final int OK_OPTION = -2;
public static final int CLOSED_OPTION = -1;
private final int HORI_MARGIN = 30;
private final int TOP_MARGIN = 20;
private final int BOTTOM_MARGIN = 10;
private final int MSG_BUTTON_INTERVAL = 15;
private final int BUTTON_WIDTH = 80;
private final int BUTTON_HEIGHT = 25;
private final int BUTTON_INTERVAL = 10;
private final int MIN_WIDTH = 200;
private final int MIN_HEIGHT = TOP_MARGIN + BOTTOM_MARGIN + MSG_BUTTON_INTERVAL + BUTTON_HEIGHT;
private SimpleButton[] buttons;
int selectedOption;
Component compMain;
public BaseDialog(Component parentComponent, Component compMain, String title, int optionType) {
super(getFrameForComponent(parentComponent), title, true);
setLayout(null);
setResizable(false);
this.compMain = compMain;
switch (optionType) {
case DEFAULT_OPTION:
buttons = new SimpleButton[1];
buttons[0] = new SimpleButton(Resource.getString(Resource.OK));
buttons[0].addActionListener(this);
break;
case YES_NO_OPTION:
buttons = new SimpleButton[2];
buttons[0] = new SimpleButton(Resource.getString(Resource.YES));
buttons[1] = new SimpleButton(Resource.getString(Resource.NO));
buttons[0].addActionListener(this);
buttons[1].addActionListener(this);
break;
case YES_NO_CANCEL_OPTION:
buttons = new SimpleButton[3];
buttons[0] = new SimpleButton(Resource.getString(Resource.YES));
buttons[1] = new SimpleButton(Resource.getString(Resource.NO));
buttons[2] = new SimpleButton(Resource.getString(Resource.CANCEL));
buttons[0].addActionListener(this);
buttons[1].addActionListener(this);
buttons[2].addActionListener(this);
break;
case OK_CANCEL_OPTION:
buttons = new SimpleButton[2];
buttons[0] = new SimpleButton(Resource.getString(Resource.OK));
buttons[1] = new SimpleButton(Resource.getString(Resource.CANCEL));
buttons[0].addActionListener(this);
buttons[1].addActionListener(this);
break;
}
Panel panel = new Panel();
panel.setLayout(null);
panel.add(compMain);
add(panel);
pack();
Dimension d = compMain.getSize();
Insets insets = getInsets();
int panelWidth, panelHeight;
if (d.width > MIN_WIDTH - HORI_MARGIN * 2) {
panelWidth = d.width + HORI_MARGIN * 2;
compMain.setLocation(HORI_MARGIN, TOP_MARGIN);
} else {
panelWidth = MIN_WIDTH;
compMain.setLocation((panelWidth - d.width) / 2, TOP_MARGIN);
}
panelHeight = MIN_HEIGHT + d.height;
panel.setBounds(insets.left, insets.top, panelWidth, panelHeight);
int width, height;
width = panelWidth + insets.left + insets.right;
height = panelHeight + insets.top + insets.bottom;
Dimension screenSize = getToolkit().getScreenSize();
setBounds((screenSize.width - width) / 2,
(screenSize.height - height) / 2,
width,
height);
int x = (panelWidth - (BUTTON_WIDTH * buttons.length + BUTTON_INTERVAL * (buttons.length - 1))) / 2;
for (int i = 0; i < buttons.length; i++) {
buttons[i].setBounds(x, panelHeight - BOTTOM_MARGIN - BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
panel.add(buttons[i]);
x += BUTTON_WIDTH + BUTTON_INTERVAL;
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
selectedOption = CLOSED_OPTION;
dispose();
}
});
}
public static int showDialog(Component parentComponent, Component compMain, String title, int optionType) {
return showOptionDialog(parentComponent, compMain, title, optionType);
}
private static int showOptionDialog(Component parentComponent, Component compMain, String title, int optionType) {
BaseDialog dialog = new BaseDialog(parentComponent, compMain, title, optionType);
dialog.setVisible(true);
return dialog.getSelectedOption();
}
private boolean fComponentsAdjusted = false;
public void addNotify() {
// 恊偺addNotify傪屇傇慜偵丄敞菽蕹偺徊睫傪曐帩偟偰偍偔
Dimension d = getSize();
super.addNotify();
if (fComponentsAdjusted)
return;
// 草警膫蓮]偭偰狠蕪叭菽埵抲傪挷愡偡傞
setSize(getInsets().left + getInsets().right + d.width,
getInsets().top + getInsets().bottom + d.height);
Component components[] = getComponents();
for (int i = 0; i < components.length; i++){
Point p = components[i].getLocation();
p.translate(getInsets().left, getInsets().top);
components[i].setLocation(p);
}
fComponentsAdjusted = true;
}
public int getSelectedOption() {
return selectedOption;
}
public void actionPerformed(ActionEvent ae) {
String command = ae.getActionCommand();
if (command.equals(Resource.getString(Resource.YES))) {
selectedOption = YES_OPTION;
} else if (command.equals(Resource.getString(Resource.NO))) {
selectedOption = NO_OPTION;
} else if (command.equals(Resource.getString(Resource.CANCEL))) {
selectedOption = CANCEL_OPTION;
} else if (command.equals(Resource.getString(Resource.OK))) {
selectedOption = OK_OPTION;
}
dispose();
}
/* private class MessageBoard extends Component {
private static final long serialVersionUID = 1L;
private final int MAX_LINE = 10;//儊僢僙乕僕偺嵟戝峴悢
private int stringHeight;
private int stringAscent;
private String[] strings = new String[MAX_LINE];
private int lineNum;
public MessageBoard() {
}
public void setMessage(String message) {
// '\r'偲'\n'傪嬫愗傝暥帤偵偡傞
StringTokenizer st = new StringTokenizer(message, "\r\n");
for (lineNum = 0; st.hasMoreTokens() && lineNum < MAX_LINE; lineNum++) {
strings[lineNum] = st.nextToken();
}
//pack(); //僐儞僥僉僗僩妋掕
Graphics g = getGraphics();
FontMetrics fm = g.getFontMetrics();
stringHeight = fm.getHeight();
stringAscent = fm.getAscent();
//暘妱偟偨暥帤楍偺嵟戝挿傪maxStringLength偵愝掕
int maxStringLength = 0;
for (int i = 0; i < lineNum; i++) {
if (fm.stringWidth(strings[i]) > maxStringLength) {
maxStringLength = fm.stringWidth(strings[i]);
}
}
setSize(maxStringLength, stringHeight * lineNum);
}
public void paint(Graphics g) {
g.setColor(Color.black);
for (int i = 0; i < lineNum; i++) {
g.drawString(strings[i], 0, stringAscent + stringHeight * i);
}
}
}*/
private static Frame getFrameForComponent(Component comp) {
Component parent = comp;
while (parent != null) {
if (parent instanceof Frame) {
break;
}
parent = parent.getParent();
}
return (Frame)parent;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -