posts - 39,  comments - 44,  trackbacks - 0

package Pro123;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Demo extends JFrame{
    /**
  *
  */
 private static final long serialVersionUID = 1L;
 JOptionPane pane1 = new JOptionPane();
 JFrame frame = new JFrame();
     Container contentPane = frame.getContentPane();
       JLabel Message = new JLabel("请输入邮箱和密码");
       //用户名、密码以及其输入框
       JLabel Name = new JLabel("用户名");
       JLabel Password = new JLabel("密   码");
       JTextField tName = new JTextField(12);
       JPasswordField pPwd = new JPasswordField(12);
       //按扭
       JButton btnDl = new JButton("登陆");
       JButton btnCz = new JButton("重置");
     
      
       JPanel pToppane = new JPanel();
       JPanel pCenterpane = new JPanel();
       JPanel pCenterpaneName = new JPanel();
       JPanel pCenterpanePwd = new JPanel();
       JPanel pButton = new JPanel();
      
       Demo(){
      
       this.setTitle("邮件登陆");//窗口标题
       
       //以下为加入面版
       pCenterpaneName.add(Name);
       pCenterpaneName.add(tName);
      
       pCenterpanePwd.add(Password);
       pCenterpanePwd.add(pPwd);
      
       pCenterpane.add(pCenterpaneName,"North");
       pCenterpane.add(pCenterpanePwd,"South");
      
       pToppane.add(Message);
     
       pButton.add(btnDl);
       pButton.add(btnCz);
       btnDl.addActionListener(new neibu1());
       btnCz.addActionListener(new neibu2());
      
       add(pToppane,"North");
       add(pCenterpane,"Center");
       add(pButton,"South");
      
      
       this.setSize(250,180);//设置大小
       this.setVisible(true);//设置可见
      
       setResizable(false);//禁止拉动窗口大小
       //居中窗口
       Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((screen.width - getSize().width)/2,(screen.height - getSize().height)/2 );
  
      // this.setDefaultCloseOperation(3);//关闭窗口
    this.addWindowListener(new WindowClose());
       }
       //对邮箱的输入格式进行判断
       public boolean validateEmailName(String name){
        String emailName = name.substring(0,name.indexOf('@'));
        boolean booFormat = true;
        int temp = -1;
        for(int i=0;i<emailName.length();i++){
         temp = name.charAt(i);
         A:if(temp<65){
          if(temp>48 && temp<57)
           break A;
          booFormat = false;
          break;
         }
         B:if(temp>90 && temp<97){
          if(temp == 95)
           break B;
          booFormat = false;
          break;
         }
        }
        return booFormat;
       }
       //邮箱检测
       public boolean validateStationName(String name){
        String stationName = name.substring(name.indexOf('@')+1);
        boolean boo = true;
        if(stationName.indexOf('.') == -1){
         boo = false;
        }else if(stationName.indexOf('.') == 0){
         boo = false;
        }
        return boo;
       }
      
       //事件
  
     
      class  neibu1 implements ActionListener {
      public void actionPerformed(ActionEvent e){
       String name = tName.getText().trim();
       String password = new String(pPwd.getPassword()).trim();
       int first = 0;
       @SuppressWarnings("unused")
   boolean booFromat = false;
       boolean boo = false;
      
       boolean booFormat = false;
   if(name.length()>0){
        first = name.charAt(0);
        if(name.indexOf("@")!= -1){
         booFormat = validateEmailName(name);
         boo = validateStationName(name);
        }
       }
       if(name.equals("")){
        Message.setForeground(Color.red);
        Message.setText("请输入邮箱");
       // tName.setText("");//清空输入内容
        pPwd.setText("");//清空输入内容
        tName.grabFocus();
       }else if(name.indexOf("@")== -1){
        Message.setForeground(Color.red);
        Message.setText("您输入邮箱格式错误,不包含“@”");
        //tName.setText("");
        pPwd.setText("");//清空输入内容
        tName.grabFocus();
       }else if(first<65 || (first>90 && first <97)||first>122){
        Message.setForeground(Color.red);
        Message.setText("邮箱首字母应为英文字母");
       // tName.setText("");
        pPwd.setText("");//清空输入内容
        tName.grabFocus();
       }else if(!booFormat){
        Message.setForeground(Color.red);
        Message.setText("邮箱名不和法:只能包含字母、数字、下划线");
        //tName.setText("");
        pPwd.setText("");//清空输入内容
        tName.grabFocus();
       }else if(!boo){
        Message.setForeground(Color.red);
        Message.setText("邮箱后缀不和法");
        //tName.setText("");
        pPwd.setText("");//清空输入内容
        tName.grabFocus();
       }else if(password.equals("")){
        Message.setForeground(Color.red);
        Message.setText("密码不能为空");
        pPwd.setText("");//清空输入内容
        pPwd.grabFocus();
       }else if(password.length()<6){
        Message.setForeground(Color.red);
        Message.setText("密码必须为6位,请检查后重新输入");
        pPwd.setText("");//清空输入内容
        pPwd.grabFocus();
       }else if(name.equals("java@svse.com")&&password.equals("jackrose")){
        Message.setForeground(Color.blue);
        Message.setText("欢迎"+name+"登陆");
       }else{
        Message.setForeground(Color.red);
        Message.setText("邮箱或密码错误!请重新输入!");
        pPwd.setText("");//清空输入内容
        tName.grabFocus();
       }
      }
       }
//    重置键清空输入内容
      class neibu2 implements ActionListener {
    
      public void actionPerformed(ActionEvent e){
       pPwd.setText("");
       tName.setText("");
      }
      }
      class WindowClose extends WindowAdapter{
      public void windowClosing(WindowEvent e){
      
      System.exit(0);
      }
      }
      //MAIN方法
       public static void main(String[]args){
        new Demo();
       }

 
}

posted on 2008-05-20 18:11 矿矿 阅读(1119) 评论(3)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: