ユーザーは例外をカスタマイズし、プログラムは例外クラスのインスタンスを作成し、放出します.プログラムを実行し、実行結果を観察します.

3431 ワード

ユーザーは例外をカスタマイズし、プログラムは例外クラスのインスタンスを作成し、放出します.プログラムを実行し、実行結果を観察します.例:ユーザーパスワードの正当化検証.パスワードには4~6個の数字が必要です.長さがこの範囲に落ちないか、数字で構成されていない場合.自分の異常を投げ出す.要求:1.オブジェクト向けプログラミング.2.異常定義、放出、スナップの3つのフロー.
package shiyan;

import java.util.*;
import javax.swing.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PassWord {
	private  String password;
	private  JButton loginButton;
	private JTextField passwordText;
	private JLabel tipLabel;
	public PassWord()
	{
		CreateWindow();
	}
	public static void main(String[] args)
	{
		PassWord zhouwei = new PassWord();
		zhouwei.action();
	    
	}
	
	public void CreateWindow() {
		
		//    
		JFrame frame = new JFrame("  ");
		frame.setSize(600, 400);//      
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    //frame.setDefaultCloseOperation()             "close"         。
	    //EXIT_ON_CLOSE(  JFrame    ):   System exit         。         。
	    
	    //    
	    JPanel panel1 = new JPanel();//           JFrame      ,             ,       。    	    
	    frame.add(panel1);//     
	    panel1.setLayout(null);//      null
	    
	    //      JLabel
	    JLabel userLabel = new JLabel("  :");
	    userLabel.setBounds(180,20,80,25);//setBounds(x, y, width, height)        ,x   y          ,  width   height       。。
	    panel1.add(userLabel);//  JLabel
	   
	    //           
	    JTextField userText = new JTextField(100);
	    userText.setBounds(210,20,165,25);
	    panel1.add(userText);//        
	    
	    //      JLabel
	    JLabel passwordLabel = new JLabel("  :");
	    passwordLabel.setBounds(180,50,80,25);
	    panel1.add(passwordLabel);

	    /* 
	     *            
	     *              ,          
	     */
	    //           
	    passwordText = new JPasswordField(100);//           
	    passwordText.setBounds(210,50,165,25);
	    panel1.add(passwordText);// //           

	    //       
	    loginButton = new JButton("  ");
	    loginButton.setBounds(250, 80, 80, 25);
	    panel1.add(loginButton);
	    
	    //     
	    tipLabel = new JLabel("");//    JLabel
	    tipLabel.setBounds(180,100,300,25);//setBounds(x, y, width, height)        ,x   y          ,  width   height       。width          
	    panel1.add(tipLabel);//  JLabel	
	    frame.setVisible(true);//       
	}
	
	//      ,try ,    
		 public void action() {
			 loginButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						password = passwordText.getText().trim();
						try {
						Check();
						tipLabel.setText("    ");
						}catch (PasswordException e1) {        
							tipLabel.setText(e1.getMessage());
						}
					}
				});
		 }
	
	//        ,     
	public void Check()throws PasswordException
		{
		    if((password.length()<4 || password.length()>6) && (!password.matches("\\d*")))
		    {
		    	throw new PasswordException("        4-6        ");
		    }
			if(password.length()<4 || password.length()>6)
			{
				throw new PasswordException("        4-6 ");
			}
			if(!password.matches("\\d*"))
			{
				throw new PasswordException("    6   ");
			}
			
		}
	
	
	
	
	//      
	public class PasswordException extends Exception
	{

		public PasswordException(String str) {
			super(str);
		}
		public PasswordException() {
	
		}
	}
}