package com.herim.java.contacts;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
/**
* 修改联系人信息
* @author Herim
*
*/
public class modifyContactorWindow extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField nameField;
private JTextField genderField;
private JTextField ageField;
private JTextField phoneField;
private JTextField emailField;
private JLabel msgLable;
private JButton addButton;
private JButton cancelButton;
private Contactor contactor;
/**
* Launch the application.
*/
/* public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new modifyContactorWindow(null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}*/
/**
* Create the frame.
*/
public modifyContactorWindow(Contactor contactor) {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 243, 297);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
msgLable = new JLabel(" ");
msgLable.setForeground(Color.RED);
GridBagConstraints gbc_msgLable = new GridBagConstraints();
gbc_msgLable.gridwidth = 4;
gbc_msgLable.insets = new Insets(0, 0, 5, 5);
gbc_msgLable.gridx = 0;
gbc_msgLable.gridy = 1;
contentPane.add(msgLable, gbc_msgLable);
JLabel nameLabel = new JLabel("Name");
GridBagConstraints gbc_nameLabel = new GridBagConstraints();
gbc_nameLabel.gridwidth = 2;
gbc_nameLabel.insets = new Insets(0, 0, 5, 5);
gbc_nameLabel.gridx = 0;
gbc_nameLabel.gridy = 2;
contentPane.add(nameLabel, gbc_nameLabel);
nameField = new JTextField();
GridBagConstraints gbc_nameField = new GridBagConstraints();
gbc_nameField.gridwidth = 2;
gbc_nameField.insets = new Insets(0, 0, 5, 0);
gbc_nameField.fill = GridBagConstraints.HORIZONTAL;
gbc_nameField.gridx = 2;
gbc_nameField.gridy = 2;
contentPane.add(nameField, gbc_nameField);
nameField.setColumns(10);
JLabel genderLabel = new JLabel("Sex");
GridBagConstraints gbc_genderLabel = new GridBagConstraints();
gbc_genderLabel.gridwidth = 2;
gbc_genderLabel.insets = new Insets(0, 0, 5, 5);
gbc_genderLabel.gridx = 0;
gbc_genderLabel.gridy = 3;
contentPane.add(genderLabel, gbc_genderLabel);
genderField = new JTextField();
GridBagConstraints gbc_genderField = new GridBagConstraints();
gbc_genderField.fill = GridBagConstraints.HORIZONTAL;
gbc_genderField.gridwidth = 2;
gbc_genderField.insets = new Insets(0, 0, 5, 0);
gbc_genderField.gridx = 2;
gbc_genderField.gridy = 3;
contentPane.add(genderField, gbc_genderField);
genderField.setColumns(10);
JLabel ageLabel = new JLabel("Age");
GridBagConstraints gbc_ageLabel = new GridBagConstraints();
gbc_ageLabel.gridwidth = 2;
gbc_ageLabel.insets = new Insets(0, 0, 5, 5);
gbc_ageLabel.gridx = 0;
gbc_ageLabel.gridy = 4;
contentPane.add(ageLabel, gbc_ageLabel);
ageField = new JTextField();
GridBagConstraints gbc_ageField = new GridBagConstraints();
gbc_ageField.gridwidth = 2;
gbc_ageField.insets = new Insets(0, 0, 5, 0);
gbc_ageField.fill = GridBagConstraints.HORIZONTAL;
gbc_ageField.gridx = 2;
gbc_ageField.gridy = 4;
contentPane.add(ageField, gbc_ageField);
ageField.setColumns(10);
JLabel phoneLabel = new JLabel("Phone");
GridBagConstraints gbc_phoneLabel = new GridBagConstraints();
gbc_phoneLabel.gridwidth = 2;
gbc_phoneLabel.insets = new Insets(0, 0, 5, 5);
gbc_phoneLabel.gridx = 0;
gbc_phoneLabel.gridy = 5;
contentPane.add(phoneLabel, gbc_phoneLabel);
phoneField = new JTextField();
GridBagConstraints gbc_phoneField = new GridBagConstraints();
gbc_phoneField.gridwidth = 2;
gbc_phoneField.insets = new Insets(0, 0, 5, 0);
gbc_phoneField.fill = GridBagConstraints.HORIZONTAL;
gbc_phoneField.gridx = 2;
gbc_phoneField.gridy = 5;
contentPane.add(phoneField, gbc_phoneField);
phoneField.setColumns(10);
JLabel emailLabel = new JLabel("Email");
GridBagConstraints gbc_emailLabel = new GridBagConstraints();
gbc_emailLabel.gridwidth = 2;
gbc_emailLabel.insets = new Insets(0, 0, 5, 5);
gbc_emailLabel.gridx = 0;
gbc_emailLabel.gridy = 6;
contentPane.add(emailLabel, gbc_emailLabel);
emailField = new JTextField();
GridBagConstraints gbc_emailField = new GridBagConstraints();
gbc_emailField.gridwidth = 2;
gbc_emailField.insets = new Insets(0, 0, 5, 0);
gbc_emailField.fill = GridBagConstraints.HORIZONTAL;
gbc_emailField.gridx = 2;
gbc_emailField.gridy = 6;
contentPane.add(emailField, gbc_emailField);
emailField.setColumns(10);
addButton = new JButton("OK");
addButton.setSize(50, 20);
addButton.addActionListener(this);
GridBagConstraints gbc_addButton = new GridBagConstraints();
gbc_addButton.weightx = 0.5;
gbc_addButton.anchor = GridBagConstraints.EAST;
gbc_addButton.ipadx = 20;
gbc_addButton.gridwidth = 3;
gbc_addButton.gridx = 0;
gbc_addButton.gridy = 8;
contentPane.add(addButton, gbc_addButton);
cancelButton = new JButton("CANCEL");
cancelButton.addActionListener(this);
GridBagConstraints gbc_cancelButton = new GridBagConstraints();
gbc_cancelButton.gridx = 3;
gbc_cancelButton.gridy = 8;
contentPane.add(cancelButton, gbc_cancelButton);
this.setVisible(true);
//初始化数据
this.contactor = contactor;
//初始化表格
if(contactor == null) {
return;
}
nameField.setText(contactor.getName());
genderField.setText(contactor.getGender());
ageField.setText(String.valueOf(contactor.getAge()));
phoneField.setText(contactor.getPhone());
emailField.setText(contactor.getEmail());
}
/**
* 监听事件
*/
@Override
public void actionPerformed(ActionEvent e) {
Pattern pattern = null;
Matcher matcher = null;
//点击ok按钮
if(e.getSource() == addButton) {
//姓名不能为空
if("".equals(nameField.getText())) {
msgLable.setText("Name can't be empty!");
return;
}
//性别不能为空
if("".equals(genderField.getText())) {
msgLable.setText("Sex can't be empty!");
return;
}
//电话不能为空
if("".equals(phoneField.getText())) {
msgLable.setText("Phone can't be empty!");
return;
}
//性别只能是男或女
if(!"男".equals(genderField.getText()) && !"女".equals(genderField.getText())) {
msgLable.setText("Sex is illegal!");
return;
}
try {
if(!"".equals(ageField.getText())) {
if(Integer.parseInt(ageField.getText()) <=0 || Integer.parseInt(ageField.getText()) >=150) {
msgLable.setText("Age is illegal!");
return;
}
}
} catch (NumberFormatException e1) {
msgLable.setText("Age is illegal!");
return;
}
/
- 1
- 2
前往页