package com.member;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
/***
* 用户编辑窗口
*
*
*/
public class UserEditDialog extends JDialog implements ActionListener {
private JTextField nameTxt;
private JTextField ageTxt;
private JTextField phoneTxt;
private JTextField addressTxt;
private JTextField usernameTxt;
private JTextField passwordTxt, birthdayTxt, postCodeTxt, otherTxt;
private User user;
private JComboBox<Object> sexBox, typeBox;
private boolean isAddFlag;
private JButton cancelBtn, submitBtn;
private UserManagerPanel userManagerPanel;
private UserList userList;
Integer userIndex;
/**
* Create the frame.
*/
public UserEditDialog(JFrame jFrame, String title, UserList userList, Integer userIndex,
UserManagerPanel userManagerPanel) {
super(jFrame, ModalityType.APPLICATION_MODAL);
this.user = userIndex == null ? null : userList.userList.get(userIndex);
this.userList = userList;
this.userIndex=userIndex;
this.userManagerPanel = userManagerPanel;
setTitle(title);
setResizable(false);
setSize(500, 500);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout(0, 0));
JPanel mainPanel = new JPanel();
mainPanel.setBorder(new EmptyBorder(20, 10, 10, 40));
getContentPane().add(mainPanel, BorderLayout.CENTER);
mainPanel.setLayout(new GridLayout(0, 1, 0, 0));
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(10, 0, 0, 0));
mainPanel.add(panel);
panel.setLayout(new GridLayout(0, 2, 0, 0));
JLabel lbl1 = new JLabel("\u59D3 \u540D\uFF1A");
lbl1.setHorizontalAlignment(SwingConstants.CENTER);
lbl1.setFont(new Font("宋体", Font.BOLD, 16));
panel.add(lbl1);
nameTxt = new JTextField();
nameTxt.setHorizontalAlignment(SwingConstants.CENTER);
nameTxt.setColumns(10);
panel.add(nameTxt);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new EmptyBorder(10, 0, 0, 0));
mainPanel.add(panel_1);
panel_1.setLayout(new GridLayout(0, 2, 0, 0));
JLabel lbl2 = new JLabel("\u5E74 \u9F84\uFF1A");
lbl2.setHorizontalAlignment(SwingConstants.CENTER);
lbl2.setFont(new Font("宋体", Font.BOLD, 16));
panel_1.add(lbl2);
ageTxt = new JTextField();
ageTxt.setHorizontalAlignment(SwingConstants.CENTER);
ageTxt.setColumns(10);
panel_1.add(ageTxt);
JPanel panel_2 = new JPanel();
panel_2.setBorder(new EmptyBorder(10, 0, 0, 0));
mainPanel.add(panel_2);
panel_2.setLayout(new GridLayout(0, 2, 0, 0));
JLabel lbl3 = new JLabel("\u6027 \u522B\uFF1A");
lbl3.setHorizontalAlignment(SwingConstants.CENTER);
lbl3.setFont(new Font("宋体", Font.BOLD, 16));
panel_2.add(lbl3);
sexBox = new JComboBox<Object>();
sexBox.setModel(new DefaultComboBoxModel<Object>(new String[] { "man", "woman" }));
sexBox.setSelectedIndex(0);
panel_2.add(sexBox);
JPanel panel_3 = new JPanel();
panel_3.setBorder(new EmptyBorder(10, 0, 0, 0));
mainPanel.add(panel_3);
panel_3.setLayout(new GridLayout(0, 2, 0, 0));
JLabel lbl4 = new JLabel("\u7535 \u8BDD\uFF1A");
lbl4.setHorizontalAlignment(SwingConstants.CENTER);
lbl4.setFont(new Font("宋体", Font.BOLD, 16));
panel_3.add(lbl4);
phoneTxt = new JTextField();
phoneTxt.setHorizontalAlignment(SwingConstants.CENTER);
phoneTxt.setColumns(10);
panel_3.add(phoneTxt);
JPanel panel_4 = new JPanel();
panel_4.setBorder(new EmptyBorder(10, 0, 0, 0));
mainPanel.add(panel_4);
panel_4.setLayout(new GridLayout(0, 2, 0, 0));
JLabel lbl5 = new JLabel("\u5730 \u5740\uFF1A");
lbl5.setHorizontalAlignment(SwingConstants.CENTER);
lbl5.setFont(new Font("宋体", Font.BOLD, 16));
panel_4.add(lbl5);
addressTxt = new JTextField();
addressTxt.setHorizontalAlignment(SwingConstants.CENTER);
addressTxt.setColumns(10);
panel_4.add(addressTxt);
JPanel panel_6 = new JPanel();
panel_6.setBorder(new EmptyBorder(10, 0, 0, 0));
mainPanel.add(panel_6);
panel_6.setLayout(new GridLayout(0, 2, 0, 0));
JLabel lbl9 = new JLabel("出生日期:");
lbl9.setHorizontalAlignment(SwingConstants.CENTER);
lbl9.setFont(new Font("宋体", Font.BOLD, 16));
panel_6.add(lbl9);
birthdayTxt = new JTextField();
birthdayTxt.setHorizontalAlignment(SwingConstants.CENTER);
birthdayTxt.setColumns(10);
panel_6.add(birthdayTxt);
JPanel panel_7 = new JPanel();
panel_7.setBorder(new EmptyBorder(10, 0, 0, 0));
mainPanel.add(panel_7);
panel_7.setLayout(new GridLayout(0, 2, 0, 0));
JLabel lbl6 = new JLabel("邮编地址:");
lbl6.setHorizontalAlignment(SwingConstants.CENTER);
lbl6.setFont(new Font("宋体", Font.BOLD, 16));
panel_7.add(lbl6);
postCodeTxt = new JTextField();
postCodeTxt.setHorizontalAlignment(SwingConstants.CENTER);
postCodeTxt.setColumns(10);
panel_7.add(postCodeTxt);
JPanel panel_8 = new JPanel();
panel_8.setBorder(new EmptyBorder(10, 0, 0, 0));
mainPanel.add(panel_8);
panel_8.setLayout(new GridLayout(0, 2, 0, 0));
JLabel lbl7 = new JLabel("其他信息:");
lbl7.setHorizontalAlignment(SwingConstants.CENTER);
lbl7.setFont(new Font("宋体", Font.BOLD, 16));
panel_8.add(lbl7);
otherTxt = new JTextField();
otherTxt.setHorizontalAlignment(SwingConstants.CENTER);
otherTxt.setColumns(10);
panel_8.add(otherTxt);
JPanel panel_10 = new JPanel();
panel_10.setBorder(new EmptyBorder(10, 0, 0, 0));
mainPanel.add(panel_10);
panel_10.setLayout(new GridLayout(0, 2, 0, 0));
JLabel lbl10 = new JLabel("客户类型:");
lbl10.setHorizontalAlignment(SwingConstants.CENTER);
lbl10.setFont(new Font("宋体", Font.BOLD, 16));
panel_10.add(lbl10);
typeBox = new JComboBox<Object>();
typeBox.setModel(new DefaultComboBoxModel<Object>(new String[] { "personal", "family","visitors" }));
typeBox.setSelectedIndex(0);
panel_10.add(typeBox);
JPanel bottomPanel = new JPanel();
bottomPanel.setBorder(new EmptyBorder(10, 0, 20, 0));
getContentPane().add(bottomPanel, BorderLayout.SOUTH);
submitBtn = new JButton("\u786E\u5B9A");
submitBtn.setFont(new Font("宋体", Font.BOLD, 16));
submitBtn.addActionListener(this);
bottomPanel.add(submitBtn);
cancelBtn = new JButton("\u53D6\u6D88");
cancelBtn.setFont(new Font("宋体", Font.BOLD, 16));
cancelBtn.addActionListener(this);
bottomPanel.add(cancelBtn);
initData();
}
void initData() {
if (user != null) {
otherTxt.setText(user.getOther());
typeBox.setSelectedIndex(Integer.parseInt(user.getUserType())-1);
addressTxt.setText(user.getAddress());
phoneTxt.setText(user.getPhone());
sexBox.setSelectedItem(user.getGender());
ageTxt.setText(user.getAge() + "");
nameTxt.setText(user.getName());
birthdayTxt.setText(user.getBirthday());
postCodeTxt.setText(user.getPostCode());
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
Component component = (Component) e.getSource();
if (component == submitBtn) {
String name = nameTxt.getText().trim();
String age = ageTxt.getText().trim();
String postCode = postCodeTxt.getText().trim();
String other = otherTxt.getText().trim();
String birthday = birthdayTxt.getText().trim();
String address = addressTxt.getText().trim();
String phone = phoneTxt.getText().trim();
if (StringUtil.isEmpty(name)) {
JOptionPane.showMessageDialog(this, "姓名不能为空!");
return;
}
if (name.length() > 12) {
JOptionPane.showMessageDialog(this