📄 commondialog.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CommonDialog
extends JDialog {
static boolean DEBUG = false;
static boolean FrameMode = true;
JPanel panel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JLabel lblMsg = new JLabel();
JPanel ctrlPanel = new JPanel();
JButton ok = new JButton();
JButton cancel = new JButton();
JButton yes = new JButton();
JButton no = new JButton();
public static final int OK = 0x1;
public static final int CANCEL = 0x2;
public static final int YES = 0x4;
public static final int NO = 0x8;
private int mode = OK;
private int result = 0;
public CommonDialog(Frame frame, String title, boolean modal, int mode) {
super(frame, title, modal);
this.mode = mode;
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
this.getContentPane().add(panel1);
pack();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void setMessage(String message) {
lblMsg.setText(" " + message + " ");
lblMsg.setSize(50, 20);
lblMsg.setFont(new java.awt.Font("宋体", 0, 12));
}
public CommonDialog(Frame frame) {
this(frame, "", false, CommonDialog.OK);
}
public CommonDialog(Frame frame, boolean modal) {
this(frame, "", modal, CommonDialog.OK);
}
public CommonDialog(Frame frame, String title) {
this(frame, title, false, CommonDialog.OK);
}
void jbInit() throws Exception {
panel1.setLayout(borderLayout1);
ok.setText(" 确 定 ");
ok.setFont(new java.awt.Font("宋体", 0, 12));
ok.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
ok_actionPerformed(e);
}
});
cancel.setText(" 取 消 ");
cancel.setFont(new java.awt.Font("宋体", 0, 12));
cancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
cancel_actionPerformed(e);
}
});
yes.setText(" 是 ");
yes.setFont(new java.awt.Font("宋体", 0, 12));
yes.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
yes_actionPerformed(e);
}
});
no.setText(" 否 ");
no.setFont(new java.awt.Font("宋体", 0, 12));
no.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
no_actionPerformed(e);
}
});
if ( (mode & OK) != 0) {
ctrlPanel.add(ok);
}
if ( (mode & YES) != 0) {
ctrlPanel.add(yes);
}
if ( (mode & NO) != 0) {
ctrlPanel.add(no);
}
if ( (mode & CANCEL) != 0) {
ctrlPanel.add(cancel);
}
panel1.add(lblMsg, BorderLayout.CENTER);
panel1.add(ctrlPanel, BorderLayout.SOUTH);
}
public static int showDialog(int mode, String message) {
return showDialog(mode, "通用对话框", message);
}
public static int showDialog(int mode, String title, String message) {
if (DEBUG) {
try {
Thread.dumpStack();
}
catch (Exception e) {
e.printStackTrace();
}
}
boolean frameMode = true;
if ( (mode & OK) != 0) {
frameMode = FrameMode;
}
CommonDialog cd = new CommonDialog(new Frame(), title, frameMode, mode);
cd.setMessage(message);
cd.pack();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
cd.setLocation( (dim.width - cd.getWidth()) / 2,
(dim.height - cd.getHeight()) / 2);
cd.show();
return cd.result;
}
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
dispose();
}
super.processWindowEvent(e);
}
void ok_actionPerformed(ActionEvent e) {
result = OK;
dispose();
}
void cancel_actionPerformed(ActionEvent e) {
result = CANCEL;
dispose();
}
void yes_actionPerformed(ActionEvent e) {
result = YES;
dispose();
}
void no_actionPerformed(ActionEvent e) {
result = NO;
dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -