继承JDialog类。在构造函数的第一句为super(owner, "title", modal);
modal为true是有模式对话框,为false是无模式对话框。
package ui.dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import ui.AdminUI;
import ui.MainClass;
import database.dataBaseBean.SparameterOperation;
import database.dataBaseBean.StaffOperation;
import database.entity.Sparameter;
import database.entity.Staff;
public class AddStaffDialog extends JDialog implements ActionListener,
PropertyChangeListener {
public JTextField staffIdTextField;
public JTextField staffNameTextField;
public JPasswordField staffPassword1TextField;
public JPasswordField staffPassword2TextField;
public JComboBox staffRoleComboBox;
public JTextField staffPhoneTextField;
public JTextField staffAddressTextField;
public JTextField staffEmailTextField;
private String OKButtonString = "确定";
private String CancelButtonString = "取消";
private JOptionPane optionPane;
private MainClass mainClass;
public AddStaffDialog(AdminUI adminUI, MainClass main) {
super(adminUI, "添加职员帐户", true);
mainClass = main;
staffAddressTextField = new JTextField(10);
staffEmailTextField = new JTextField(10);
staffIdTextField = new JTextField(10);
staffNameTextField = new JTextField(10);
staffPassword1TextField = new JPasswordField(10);
staffPassword2TextField = new JPasswordField(10);
staffPhoneTextField = new JTextField(10);
staffRoleComboBox = new JComboBox(Staff.LEVEL);
String idString = "职员编号:";
String nameString = "职员姓名:";
String passwordString = "输入密码:";
String ensurePasswordString = "密码确认:";
String roleString = "职员权限:";
String addressString = "家庭住址:";
String phoneString = "联系电话:";
String emailString = "电子邮件:";
Object array[] = { idString, staffIdTextField, nameString,
staffNameTextField, passwordString, staffPassword1TextField,
ensurePasswordString, staffPassword2TextField, roleString,
staffRoleComboBox, phoneString, staffPhoneTextField,
addressString, staffAddressTextField, emailString,
staffEmailTextField };
Object options[] = { OKButtonString, CancelButtonString };
optionPane = new JOptionPane(array, JOptionPane.PLAIN_MESSAGE,
JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);
setContentPane(optionPane);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
/*
* Instead of directly closing the window, we're going to change
* the JOptionPane's value property.
*/
optionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
// change the JOption's value before closing
optionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
}
});
optionPane.addPropertyChangeListener(this);
addComponentListener(new ComponentAdapter() {
public void componentShown(ComponentEvent ce) {
staffIdTextField.requestFocusInWindow();
}
});
setSize(320, 450);
setResizable(false);
setVisible(true);
}
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if ((e.getSource() == optionPane)
&& (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY
.equals(prop))) {
Object value = optionPane.getValue();
if (value == JOptionPane.UNINITIALIZED_VALUE) {
// ignore reset
return;
}
// Reset the JOptionPane's value.
// If we don't do this, then if the user
// presses the same button next time, no
// property change event will be fired.
// optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
if (OKButtonString.equals(value)) {
String id = staffIdTextField.getText();
String name = staffNameTextField.getText();
String password1=new String(staffPassword1TextField.getPassword());
String password2=new String(staffPassword2TextField.getPassword());
String role=(String)staffRoleComboBox.getSelectedItem();
String phone=staffPhoneTextField.getText();
String address=staffAddressTextField.getText();
String email=staffEmailTextField.getText();
// TODO add staff
if(password1.equals(password2)==false)
JOptionPane.showMessageDialog(null,"密码输入错误","错误",JOptionPane.ERROR_MESSAGE);
StaffOperation.insertStaff(mainClass.connection,new Staff(
id,name,password1,role,phone,address,email));
}
setVisible(false);
}
}
public void actionPerformed(ActionEvent e) {
optionPane.setValue(OKButtonString);
}
}