Java--正则表达式及应用

首次尝试用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, "输入合法");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值