首次尝试用java编写图形界面;还有很多不熟悉的地方;
希望理解透这第一份;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Created by yehao on 15-3-6.
*/
public class RegexStringText extends JFrame{
private JTextField phoneTextField,zipTextField,addressTextField,
firstTextField, lastTextField, chineseTextField;
private Font songTi = new Font("宋体",Font.PLAIN,12);
public RegexStringText() {
super("基于字符串的正则表达式");
//创建图形界面
JLabel phoneLabel = new JLabel("电话");
phoneLabel.setFont(songTi);
JLabel zipLabel = new JLabel("邮政编码");
zipLabel.setFont(songTi);
JLabel addressLabel = new JLabel("通信地址");
addressLabel.setFont(songTi);
JLabel firstLabel = new JLabel("First Name: (英文,第一个字母必须大写)");
firstLabel.setFont(songTi);
JLabel lastLabel = new JLabel("Last Name : (英文,第一个字母要必须大写");
lastLabel.setFont(songTi);
JLabel chineseLabel = new JLabel("中文");
chineseLabel.setFont(songTi);
JButton okButton = new JButton("验证");
okButton.setFont(songTi);
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
vaildataData();
}
});
phoneTextField = new JTextField(15);
zipTextField = new JTextField(6);
addressTextField = new JTextField(35);
firstTextField = new JTextField(20);
lastTextField = new JTextField(20);
chineseTextField = new JTextField(30);
JPanel firstName = new JPanel();
firstName.add(firstLabel);
firstName.add(firstTextField);
JPanel lastName = new JPanel();
lastName.add(lastLabel);
lastName.add(lastTextField);
JPanel address = new JPanel();
address.add(addressLabel);
address.add(addressTextField);
JPanel zipAndPhone = new JPanel();
zipAndPhone.add(zipLabel);
zipAndPhone.add(zipTextField);
zipAndPhone.add(phoneLabel);
zipAndPhone.add(phoneTextField);
JPanel chinese = new JPanel();
chinese.add(chineseLabel);
chinese.add(chineseTextField);
JPanel ok = new JPanel();
ok.add(okButton);
//把GUI部件添加到容器中
Container container = getContentPane();
container.setLayout(new GridLayout(6,1));
container.add(firstName);
container.add(lastName);
container.add(address);
container.add(zipAndPhone);
container.add(chinese);
container.add(ok);
pack();
setVisible(true);
} //RegexStringText 构造器结束
public static void main(String[] arge) {
RegexStringText application = new RegexStringText();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void vaildataData() {
if(lastTextField.getText().equals("") || firstTextField.getText().equals("")
|| addressTextField.getText().equals("") ||zipTextField.getText().equals("")
|| phoneTextField.getText().equals("") || phoneTextField.getText().equals("")
|| chineseTextField.getText().equals("")) {
JOptionPane.showMessageDialog(this, "所有栏都要填");
}
else if(!firstTextField.getText().matches("[A-Z][a-zA-Z]*")) {
//[A-Z]表示匹配一个大写字母[a-zA-Z]*表示匹配多个字母*表示多个
JOptionPane.showMessageDialog(this, "First Name非法,首字母大写");
}
else if(!lastTextField.getText().matches("[A-Z][a-zA-Z]*")) {
JOptionPane.showMessageDialog(this, "Last Name非法,首字母大写");
}
else if(!zipTextField.getText().matches("\\d{5}")) {
//输入为5个数字
JOptionPane.showMessageDialog(this, "邮政编码非法");
}
else if(!phoneTextField.getText().matches("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}")) {
//[1-9]\\d{2}表示匹配三个数字,但第一个不为0;这个格式为123-123-1111
JOptionPane.showMessageDialog(this, "电话号码非法");
}
else if(!chineseTextField.getText().matches("[\u4E00-\u9FA5]")) {
//Unicode编码中处于4E00-9FA5中间的字符(即中文)
JOptionPane.showMessageDialog(this, "只能输入中文");
}
else
JOptionPane.showMessageDialog(this, "输入合法");
}
}