用于验证的Java小程序一则

开发 后端
此程序是用于验证用户输入的姓名、E-mail和职业是否合法的Java小程序。

在界面上加一个”确定”的按钮和文本区域。在单击按钮的时候,程序验证用户是否输入了合法的E-Mail 地址格式,如果验证结果合法就将用户输入的姓名、E-mail和职业尾加到文本区中,验证不合法则在输入E-MAIL 的文本框中提示用户输入了非法格式的E-MAIL地址。


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.Container;
public class test{
    public static void main(String args[]){
        new WindowBox();
    }
}
class WindowBox extends JFrame{
   Box baseBox,boxV1,boxV2,boxV3;
   JTextField a,b,c;
   JButton d;
   JTextArea e;
    WindowBox(){
    
        boxV1=Box.createVerticalBox();
        boxV1.add(new JLabel("姓名"));
        boxV1.add(Box.createVerticalStrut(8));
        boxV1.add(new JLabel("email"));
        boxV1.add(Box.createVerticalStrut(8));
        boxV1.add(new JLabel("职业"));
       

        boxV2=Box.createVerticalBox();
        a=new JTextField(16);
        boxV2.add(a);
        boxV2.add(Box.createVerticalStrut(8));
 b=new JTextField(16);      
        boxV2.add(b);
        boxV2.add(Box.createVerticalStrut(8));
        c=new JTextField(16);
        boxV2.add(c);

        baseBox=Box.createHorizontalBox();
        baseBox.add(boxV1);
        baseBox.add(Box.createHorizontalStrut(10));
        baseBox.add(boxV2);
 
        boxV3=Box.createVerticalBox();       
        d=new JButton("确定");
        boxV3.add(d);
        e=new JTextArea(16,16);
        d.addActionListener(new MyMonitor(this));
        boxV3.add(e);       
        setLayout(new FlowLayout());
  add(baseBox);
  add(boxV3);
        validate();
        setBounds(120,125,300,500);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}
class MyMonitor implements ActionListener {

           WindowBox jf=null;
     public MyMonitor (WindowBox jf)
{
   this.jf=jf;
}
  public void actionPerformed(ActionEvent e)
  {
         String x=jf.a.getText();
         String y=jf.b.getText();
         String z=jf.c.getText();
   if(y.indexOf("@")!=-1)
   {
         jf.e.setText("姓名:"+x+"  "+"EMAIL:"+y+"  "+"职业:"+z);
   }
   else
   {
    jf.b.setText("输入的格式不正确");
   }
 
 }
}

以上便是该用于验证用户输入的姓名、E-mail和职业是否合法的Java小程序的整体。


 

【编辑推荐】

  1. 对Java编程思想的忠告
  2. 和我共同了解Java是什么
  3. 著名的Java论坛和网站
  4. 2009年十大Java技术解决方案
  5. 2008最值得学习的五种JAVA技术
责任编辑:仲衡 来源: CSDN博客
相关推荐

2011-04-11 09:53:06

Oracle

2011-05-27 10:02:42

Shell

2013-03-11 10:37:08

2009-10-21 09:58:28

桌面LinuxLinux操作系统

2009-06-11 16:11:26

Java运算器一则运算

2010-07-21 16:53:33

telnet命令

2010-07-26 15:14:04

telnet服务

2023-11-28 16:35:40

MongoDB迁移Oplog

2023-04-30 00:02:40

K8Skubelet数据

2012-04-18 18:12:47

iPad Mini

2019-04-01 07:51:21

2019-12-03 10:40:25

LinuxCPU网络调优

2012-05-03 09:56:31

招聘开发PHP

2019-11-04 14:25:54

大数据Hadoop大数据时代

2022-02-27 10:13:42

分包Uiniapp项目

2016-09-27 17:29:23

腾讯云小程序微信

2022-06-05 08:09:21

CollisionLinux 应用Linux

2010-04-20 10:00:29

2012-03-19 09:31:44

程序员

2012-03-09 17:30:28

ibmdw
点赞
收藏

51CTO技术栈公众号