• 【作业】三个关于java的探索和两个实验题


    关于枚举

    (1)

    Size s=Size.SMALL;

    Size t=Size.LARGE;

    False

    False

    true

    SMALL

    MEDIUM

    LARGE

    (2)

    Size s=Size.SMALL;

    Size t=Size.SMALL;

    true

    false

    true

    SMALL

    MEDIUM

    LARGE

    s和t不是引用同一个对象

    属于原始数据类型

    验证同名覆盖

    public class text_002_tongming

    {

           private static int i=10;

           public static void main(String[] args)

           {

                  int i=5;

                  System.out.println(i);

           }

    }

    输出:

    5

    Double不精确的分析

    0.05 + 0.01 = 0.060000000000000005

    1.0 - 0.42 = 0.5800000000000001

    4.015 * 100 = 401.49999999999994

    123.3 / 100 = 1.2329999999999999

    Double 占8字节,共计64位,。由最高到最低位分别是第63、62、61、……、0位;

    63位是符号位,1表示该数为负,0正;

    62-52位,一共11位是指数位;

    51-0位,一共52位是尾数位。

    对于0.05,化为二进制

    0000 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100

    0000 0000 0000 0000 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100

    0000 0000 0000 0000 0010 1000 1111 0101 1100 0010 1000 1111 0101 1100 0010 1000

    0000 0000 0000 0000 1111 0101 1100 0010 1000 1111 0101 1100 0010 1000 1111 0100

    小数部分:

    0000 1111 0101 1100 0010 1000 1111 0101 1100 0010 1000 1111 0100

    0

    0.05859375

    0.059814453125

    0.05999755859375

    0.0599994659423828125

    0.25999942779541015625

    0.0599999986588954925537109375

    0.05999999982304871082305908203125

    0.05999999999767169356346130371094

    0.05999999999949068296700716018677

    0.05999999999994543031789362430573

    0.0599999999999872102307563181967

    0.0599999999999996092014953319449 

    约等于 0.060000000000000610

    似乎差了点……总之是不准!

    小学四则运算出题程序

      1 package Equation;
      2 
      3 import java.math.*;
      4 
      5 //1.30
      6 //加减乘除,二到三的长度,乘法和除法,
      7 
      8 public class Equation
      9 {
     10     private int equLong;
     11     private int equation[]=new int[3];// 数字
     12     private char symbol[]=new char[2];//符号
     13     private int answer;//答案
     14     public Equation()
     15     {
     16         equLong=0;
     17         answer=0;
     18     }
     19     public void setEquLong(int equLong)
     20     {
     21         this.equLong=equLong;
     22     }
     23     public int getEquLong()
     24     {
     25         return equLong;
     26     }
     27     public void setEquation(int a,int b)
     28     {
     29         equation[0]=a;
     30         equation[1]=b;
     31     }
     32     public void setEquation(int a,int b,int c)
     33     {
     34         equation[0]=a;
     35         equation[1]=b;
     36         equation[2]=c;
     37     }
     38     public void setSymbol(char a)
     39     {
     40         symbol[0]=a;
     41     }
     42     public void setSymbol(char a,char b)
     43     {
     44         symbol[0]=a;
     45         symbol[1]=b;
     46     }
     47     public void setAnswer(int answer)
     48     {
     49         this.answer=answer;
     50     }
     51     public int getAnswer()
     52     {
     53         return answer;
     54     }
     55     public int caculate()
     56     {
     57         double d=0,e=0;
     58         String s="",s1="";
     59         Caculate ca=new Caculate();
     60         s1=String.valueOf(equation[0]);//
     61         s=s+s1;
     62         for(int i=0;i<equLong-1;i++)
     63         {
     64             s1=String.valueOf(symbol[i]);//
     65             s=s+s1;
     66             s1=String.valueOf(equation[i+1]);//
     67             s=s+s1;
     68         }
     69         
     70         ca.setStr(s);
     71         d=ca.output();
     72         //System.out.println(s+"="+d);
     73         /*if(Math.abs(d-(int)d)<0.000001&&Math.abs(d)<500)*/
     74         if(Math.abs(d-(int)d)<0.000001&&d<500&&d>=0)//控制答案的范围
     75         {
     76             answer=(int)d;
     77             return 1;
     78         }
     79         else
     80         {
     81             return -1;
     82         }
     83     }
     84     public void showEquation()
     85     {
     86         
     87         System.out.print(equation[0]+" ");
     88         System.out.print(symbol[0]+" ");
     89         System.out.print(equation[1]+" ");
     90         if(equLong==2)
     91         {
     92             System.out.println("=");
     93         }
     94         else
     95         {
     96             System.out.print(symbol[1]+" ");
     97             System.out.print(equation[2]+" ");
     98             System.out.println("=");
     99         }
    100 
    101     }
    102     public void showAnswer()
    103     {
    104         System.out.println(answer);
    105     }
    106 }
    Equation.java
     1 package Equation;
     2 import java.util.Scanner;
     3 import java.util.Random;
     4 public class SetEquation
     5 {
     6 
     7     public static void main(String[] args)
     8     {
     9         Scanner input=new Scanner(System.in);
    10         int number=60,i=0,t=0,equLong=0,a=0,b=0;
    11         char c='+',d='+';
    12         Equation equ[]=new Equation[number];
    13         String an="";
    14         Random r=new Random();
    15         /*System.out.println("num??");
    16         try
    17         {
    18         number=input.nextInt();//设置题数
    19         }
    20         catch(Exception e)
    21         {
    22             System.out.println("请输入正整数!");
    23         }*/
    24         i=0;
    25         while(i<number)
    26         {
    27             equ[i]=new Equation();
    28             equ[i].setEquLong(r.nextInt(2)+2);//
    29         //    System.out.println(equ[i].getEquLong());
    30             if(equ[i].getEquLong()==2)
    31             {
    32                 equ[i].setEquation(r.nextInt(100)+1, r.nextInt(100)+1);
    33                 a=r.nextInt(4)+1;
    34                 switch(a)
    35                 {
    36                 case 1:case 2:case 3:case 4:c='+';break;
    37                 case 5:case 6:case 7:case 8:c='-';break;
    38                 case 9:c='*';break;
    39                 case 10:c='/';break;
    40                 }
    41                 equ[i].setSymbol(c);
    42             }
    43             else
    44             {
    45                 equ[i].setEquation(r.nextInt(100)+1, r.nextInt(50)+1,r.nextInt(100)+1);
    46                 a=r.nextInt(10)+1;
    47                 b=r.nextInt(10)+1;
    48                 switch(a)
    49                 {
    50                 case 1:case 2:case 3:case 4:c='+';break;
    51                 case 5:case 6:case 7:case 8:c='-';break;
    52                 case 9:c='*';break;
    53                 case 10:c='/';break;
    54                 }
    55                 switch(b)
    56                 {
    57                 case 1:d='+';break;
    58                 case 2:d='-';break;
    59                 case 3:d='*';break;
    60                 case 4:d='/';break;
    61                 }
    62                 equ[i].setSymbol(c, d);
    63 
    64             }
    65             if(equ[i].caculate()==1)
    66             {
    67                 System.out.print("("+(i+1)+")	");
    68             equ[i].showEquation();
    69             
    70             i++;
    71             }
    72         }
    73         System.out.println("按 A 显示答案");
    74         an=input.next();
    75         if(an.equals("A"))
    76         {
    77             t=0;
    78             while(t<i)
    79             {
    80                 System.out.print("("+(t+1)+")	");
    81                 equ[t].showAnswer();
    82                 t++;
    83             }
    84         }
    85     }
    86     
    87 }
    SetEquation.jzva

    没错!我自己抄了自己的计算器程序来计算结果!

      1 package Equation;
      2 
      3 import java.util.ArrayList;
      4 
      5 public class Caculate
      6 {
      7     private String strIn;
      8     private double result;//总计算结果
      9     /*public static void main(String[] args)
     10     {
     11         Scanner input=new Scanner(System.in);
     12         double result;
     13         String strIn;//="1+(2-3*(5+1)/(-4+2*(-6))-3*(+6-(-2)))+6*(5-4)";
     14         System.out.println("请输入需要计算的式子:(不要有空格)");
     15         strIn=input.nextLine();
     16         center(strIn);
     17        
     18         input.close();
     19     }*/
     20     public void setStr(String strIn)
     21     {
     22         this.strIn=strIn;
     23         
     24     }
     25     public  void center()//计算器主体
     26     {
     27         //计算前进行验证处理
     28         judge(strIn);
     29         String str0=strIn;//留存备份
     30         strIn=minus(strIn);//负号
     31         strIn=plus(strIn);//正号
     32         strIn=addBrackets(strIn);//保持最外围有括号
     33         ArrayList<String> jiSuan1=new ArrayList<String>();
     34         ArrayList<Double> jiSuan2=new ArrayList<Double>();
     35         ArrayList<Double> re=new ArrayList<Double>();
     36         ArrayList<Double> num=separateNum(strIn);//分离数字
     37         ArrayList<Integer> serNum=new ArrayList<Integer>();
     38         ArrayList<String>  sym=separateSym(strIn);//分离运算符
     39         serNum=serialNumber(sym);//统计数字
     40         
     41         int cm=brackets0(sym);//总括号数
     42         int bra [][]=new int[cm][2];
     43         bra=brackets(sym);//括号统计
     44         
     45         int m=0;//m 最大层数
     46         for(int i=0;i<bra.length;i++)
     47         {
     48             if(m<bra[i][0])
     49             {
     50                 m=bra[i][0];
     51             }
     52         }
     53         int i=0,k,t=0,d,f=1,g=1,n,r1=-1,r2=0,l1=-1,l2=-1;//t,d 相对应的一对括号
     54         for(i=m;i>=0;i--)//层数循环
     55         {
     56             for(t=0;t<bra.length;t++)
     57             {
     58                 if(bra[t][0]==i)//每一对括号
     59                 {
     60                     d=t;
     61                     jiSuan1.clear();//清空
     62                     jiSuan2.clear();
     63                     re.clear();
     64                     for(;;)//找出相对应的另一半括号
     65                     {
     66                         d++;
     67                         if(bra[d][0]==bra[t][0])
     68                         {
     69                             break;
     70                         }
     71                     }
     72                     l1=bra[t][1];
     73                     r1=bra[d][1];
     74                     for(k=bra[t][1]+2;k<bra[d][1];k+=2)//将需要计算的部分输入jiSuan1中
     75                     {
     76                         
     77                         jiSuan1.add(sym.get(k));
     78                         /*if(g1==1)
     79                         {
     80                             k1=k;
     81                             g1=0;
     82                         }*/
     83                     }
     84                 //    sym.add(bra[t][0],"0");
     85                     for(k=0,g=1,l2=0,r2=0;k<serNum.size();k++)//将需要计算的部分输入jiSuan2中serNum.get(k)
     86                     {
     87                         n=serNum.get(k);
     88                         if(n>bra[t][1]&&n<bra[d][1])
     89                         {
     90                             jiSuan2.add(num.get(k));
     91                             r2++;
     92                         if(g==1)
     93                             {
     94                                 l2=k;
     95                                 g=0;
     96                             }
     97                         }
     98                     }
     99                     for(int x=l1;x<=r1&&l1>=0&&r1>=0;x++)
    100                     {
    101                         sym.set(x, "@");
    102                     }
    103                     //调用函数计算
    104                     result=caculate(jiSuan1,jiSuan2);
    105                     //删除
    106                     int z=l1;
    107                     while(z>=0)
    108                     {
    109                         sym.remove(z);
    110                         z=sym.indexOf("@");
    111                         
    112                     }
    113                     sym.add(l1,"0");
    114                     for(z=0;z<r2;z++)
    115                     {
    116                         num.remove(l2);
    117                     }
    118                     num.add(l2, result);
    119                     //计算
    120                     bra=brackets(sym);
    121                     serNum=serialNumber(sym);
    122                     //System.out.println(d);
    123                     t=0;
    124                 }
    125             }
    126         }
    127         //System.out.println("result***"+result);
    128     }
    129 
    130     private  ArrayList<String> sToA(String s)  //将 String 转化为 ArrayList
    131     {
    132        ArrayList<String> a=new ArrayList<String>();
    133        for(int i=0;i<s.length();i++)
    134        {
    135            a.add(s.substring(i, i+1));
    136        }
    137        return a;
    138     
    139     }
    140     private  String aToS(ArrayList<String> a)  //将 String 转化为 ArrayList
    141     {
    142        String s="";
    143        for(int i=0;i<a.size();i++)
    144        {
    145            s=s+a.get(i);
    146        }
    147       return s;
    148     }
    149     
    150     private  String minus(String s)//对负数进行处理:前面加0
    151     {
    152         String a,b;
    153           int i=1;
    154           if(s.charAt(0)=='-')
    155           {
    156               s="0"+s;
    157           }
    158           while(i!=-1)
    159         {
    160             i=s.indexOf('-', i);
    161             if(i>=0)
    162             {
    163                 
    164                 if(i>0&&s.charAt(i-1)=='(')
    165                 {
    166                 a=s.substring(0, i);
    167                 b=s.substring(i);
    168                 s=a+"0"+b;
    169                 i+=2;
    170                 }
    171                 else
    172                 {
    173                     i++;
    174                 }
    175                 
    176             }
    177             else
    178                 break;
    179             //System.out.println(s);
    180         }
    181         return s;
    182     }
    183     private  String plus(String s)//对显式写出的正数进行处理:前面加0
    184     {
    185         String a,b;
    186           int i=1;
    187           if(s.charAt(0)=='+')
    188           {
    189               s="0"+s;
    190           }
    191           while(i!=-1)
    192         {
    193             i=s.indexOf('+', i);
    194             if(i>=0)
    195             {
    196                 
    197                 if(i>0&&s.charAt(i-1)=='(')
    198                 {
    199                 a=s.substring(0, i);
    200                 b=s.substring(i);
    201                 s=a+"0"+b;
    202                 i+=2;
    203                 }
    204                 else
    205                 {
    206                     i++;
    207                 }
    208                 
    209             }
    210             else
    211                 break;
    212             //System.out.println(s);
    213         }
    214         return s;
    215     }
    216     private  String addBrackets(String s)
    217     {
    218         if(!(s.charAt(0)=='('&&s.charAt(s.length()-1)==')'))
    219         {
    220             s="("+s+")";
    221         }
    222         return s;
    223     }
    224     private  int brackets0(ArrayList<String> str)   //实现括号的粗略统计
    225     {
    226         ArrayList<String> s=new ArrayList<String>(str);
    227         int c=0,i=0;
    228         for(;;)
    229         {
    230             if((i=s.indexOf("("))<0)
    231             {
    232                 break;
    233             }
    234             s.remove(i);
    235                 c++;
    236         }
    237         for(;;)
    238         {
    239             if((i=s.indexOf(")"))<0)
    240             {
    241                 break;
    242             }
    243             s.remove(i);
    244                 c++;
    245         }
    246             return c;
    247     }
    248     
    249     private  ArrayList<Integer> serialNumber(ArrayList<String> s) //实现数字的统计
    250     {
    251         ArrayList<Integer> a=new ArrayList<Integer>();
    252         int i=0;
    253         String str;
    254         char c;
    255         for(i=0;i<s.size();i++)
    256         {
    257             str=s.get(i);
    258             c=str.charAt(0);
    259             if(c>='0'&&c<='9')
    260             {
    261                 a.add(i);
    262             }
    263             
    264         }
    265         return a;
    266     }
    267     private  int[][] brackets( ArrayList<String> sym) //实现括号的统计
    268     {
    269        //                   +(-*(+)/(-+*(-))-*(+-(-)))+*(-)
    270        ArrayList<Integer> b1=new ArrayList<Integer>();//层数
    271        ArrayList<Integer> b2=new ArrayList<Integer>();//位置
    272        int c=-1;//层数
    273        int cm=0;//最大层数
    274        int i,f=1;
    275       String s=aToS(sym);
    276        for( i=0;i<s.length();i++)
    277        {
    278        
    279            if(s.charAt(i)=='(')
    280            {
    281                if(f==1)
    282                {
    283                c++;
    284                }
    285                f=1;
    286                b1.add(c);
    287             b2.add(i);
    288            }
    289            if(s.charAt(i)==')')
    290            {
    291                if(f==0)
    292                {
    293                c--;
    294                }
    295                f=0;
    296                b1.add(c);
    297             b2.add(i);
    298            }
    299            if(cm<c)
    300            {
    301                cm=c;
    302            }
    303            
    304        }
    305        
    306 
    307              int bra[][]=new int[b1.size()][2];//第一 维序号,第二维层数、位置
    308              for(i=0;i<b1.size();i++)
    309              {
    310                  bra[i][0]=b1.get(i);
    311                  bra[i][1]=b2.get(i);
    312              }
    313          
    314         return bra;
    315     
    316     }
    317     
    318    private  double caculate(ArrayList<String> s,ArrayList<Double> a) //计算
    319    
    320     {
    321        double result=0,left,right;
    322        int i=0;
    323        while(i>=0)
    324        {
    325                i=s.indexOf("/");
    326                if(i<0)break;
    327                left=a.remove(i);
    328                right=a.remove(i);
    329                try
    330                {
    331                    if(Math.abs(right)<10e-8)
    332                    {
    333                        throw new Exception("除数不能为零!");
    334                    }
    335                    a.add(i, left/right);
    336                }
    337                catch(Exception e)
    338                {System.out.println(e.getMessage());
    339                }
    340                s.remove(i);
    341        }
    342        i=0;
    343           while(i>=0)
    344           {
    345               i=s.indexOf("*");
    346               if(i<0)break;
    347               left=a.remove(i);
    348               right=a.remove(i);
    349               a.add(i, left*right);
    350               s.remove(i);
    351           }
    352           i=0;
    353           while(i>=0)
    354           {
    355               i=s.indexOf("-");
    356               if(i<0)break;
    357               left=a.remove(i);
    358               right=a.remove(i);
    359               a.add(i, left-right);
    360               s.remove(i);
    361           }
    362           i=0;
    363           while(i>=0)
    364           {
    365               i=s.indexOf("+");
    366               if(i<0)break;
    367               left=a.remove(i);
    368               right=a.remove(i);
    369               a.add(i, left+right);
    370               s.remove(i);
    371           }
    372        
    373     //end
    374            result=a.get(0);
    375         return result;
    376     }
    377 
    378     
    379     private  ArrayList<Double> separateNum(String s) 
    380     {
    381         ArrayList<Double> num=new ArrayList<Double>();
    382         String c="";
    383         int i=0,t=0,f=0,l;
    384         double d=0,a;
    385         for(i=0;i<s.length();i++)
    386         {
    387             if(s.charAt(i)>='0'&&s.charAt(i)<='9')
    388             {
    389                 c=c+s.charAt(i);
    390                 f=1;
    391             //    System.out.println("add"+c);
    392             }
    393             else if(f==1)
    394             {
    395                 //字符转数字
    396                 l=c.length();
    397                 for(t=0,d=0;t<l;t++)
    398                 {
    399                     a=c.charAt(t)-'0';
    400                     
    401                     d=d+a*Math.pow(10,l-1-t);    
    402                     
    403                 }
    404   
    405                 num.add(d);
    406                 f=0;
    407                 c="";
    408             }
    409         }
    410  
    411         return num;
    412     }
    413     private  ArrayList<String> separateSym(String s)
    414     {
    415         ArrayList<String> sym=new ArrayList<String>();
    416         int f=1;
    417         if(s.charAt(0)<'0'||s.charAt(0)>'9')
    418         {
    419             s="0+"+s;
    420         }
    421             for(int i=0;i<s.length();i++)
    422             {
    423                 if(s.charAt(i)>='0'&&s.charAt(i)<='9')
    424                 {
    425                     f=1;
    426                 }
    427                 else if(f==1)
    428                 {
    429                     sym.add("0");
    430                     f=0;
    431                 }
    432                 if(s.charAt(i)=='+'||s.charAt(i)=='-'||s.charAt(i)=='*'||s.charAt(i)=='/'||s.charAt(i)=='('||s.charAt(i)==')')
    433                 {
    434                     sym.add(s.substring(i,i+1));
    435                 }
    436                     
    437             }
    438             //System.out.println(sym);
    439             sym.remove(0);
    440             sym.remove(0);
    441         return sym;
    442     }
    443     private  void judge(String s)//验证式子是否正确
    444     {
    445         try
    446         {
    447             //字符是否正确
    448             for(int i=0,f=0;i<s.length();i++)
    449             {
    450                 f=0;
    451                 if(s.charAt(i)=='+'||s.charAt(i)=='-'||s.charAt(i)=='*'||s.charAt(i)=='/'||s.charAt(i)=='('||s.charAt(i)==')')
    452                 {
    453                     f=1;
    454                 }
    455                 if(s.charAt(i)>='a'&&s.charAt(i)<='z'||s.charAt(i)>='A'&&s.charAt(i)<='Z')
    456                 {
    457                     f=1;
    458                 }
    459                 if(s.charAt(i)>='0'&&s.charAt(i)<='9')
    460                 {
    461                     f=1;
    462                 }
    463                 if(f==0)throw  new Exception("未识别的符号" "+s.charAt(i)+" ",位置:"+(i+1));
    464             }
    465             //括号是否匹配
    466             int left=0,right=0;
    467         for(int i=0;i<s.length();i++)
    468         {
    469             if(s.charAt(i)=='(')
    470             {
    471                 left++;
    472             }
    473             if(s.charAt(i)==')')
    474             {
    475                 right++;
    476             }
    477             
    478         }
    479         if(left!=right)throw  new Exception("括号不匹配");
    480         //符号是否正确
    481         int f=1;
    482         if(s.charAt(0)=='-')
    483         {
    484             s="0"+s;
    485             f=0;
    486         }
    487         for(int i=0;i<s.length();i++)
    488         {
    489             if(s.charAt(i)=='+'||s.charAt(i)=='-')
    490             {
    491                 if(s.charAt(i-1)=='+'||s.charAt(i-1)=='-'||s.charAt(i-1)=='*'||s.charAt(i-1)=='/')
    492                 {
    493                     throw  new Exception("运算符"+s.charAt(i)+"左边没有数字,位置:"+(i+f));
    494                 }
    495                 if(s.charAt(i+1)=='+'||s.charAt(i+1)=='-'||s.charAt(i+1)=='*'||s.charAt(i+1)=='/'||s.charAt(i+1)==')')    
    496                 {
    497                     throw  new Exception("运算符"+s.charAt(i)+"右边没有数字,位置:"+(i+f));
    498                 }
    499                 if(s.charAt(s.length()-1)=='+'||s.charAt(s.length()-1)=='-'||s.charAt(s.length()-1)=='*'||s.charAt(s.length()-1)=='/')    
    500                 {
    501                     throw  new Exception("运算符"+s.charAt(s.length()-1)+"右边没有数字,位置:"+(s.length()-1+f));
    502                 }
    503             }
    504             if(s.charAt(i)=='*'||s.charAt(i)=='/')
    505             {
    506                 if(s.charAt(i-1)=='+'||s.charAt(i-1)=='-'||s.charAt(i-1)=='*'||s.charAt(i-1)=='/'||s.charAt(i-1)=='(')
    507                 {
    508                     throw  new Exception("运算符"+s.charAt(i)+"左边没有数字,位置:"+(i+f));
    509                 }
    510                 if(s.charAt(i+1)=='+'||s.charAt(i+1)=='-'||s.charAt(i+1)=='*'||s.charAt(i+1)=='/'||s.charAt(i+1)==')')    
    511                 {
    512                     throw  new Exception("运算符"+s.charAt(i)+"右边没有数字,位置:"+(i+f));
    513                 }
    514                 if(s.charAt(s.length()-1)=='+'||s.charAt(s.length()-1)=='-'||s.charAt(s.length()-1)=='*'||s.charAt(s.length()-1)=='/')    
    515                 {
    516                     throw  new Exception("运算符"+s.charAt(s.length()-1)+"右边没有数字,位置:"+(s.length()-1+f));
    517                 }
    518             }
    519         }
    520         }catch(Exception e)
    521         {
    522             System.out.println(e.getMessage());
    523         }
    524     }
    525     public double output()
    526     {
    527         center();
    528         //System.out.println(strIn+" = "+result);
    529         return result;
    530     }
    531 }
    Caculate.java

    非常不完美的用户登陆且输入验证码界面

    国庆稍微玩了一下就没了,于是熬夜在最后的晚上弄完了,算是可以实现所需要的功能的程序

    参考了老师的随机生成字符串程序,从网上找的关于swing的教程、焦点事件、如何画出一个验证码

    问题:

    1、在JButton事件中的

    Button1.addActionListener(new ActionListener()
    {

    @Override
    public void actionPerformed(ActionEvent arg0)
    {

    //这里面究竟要怎么和外面互相传值啊   啊   啊  ……

    }

    });

    2、没法愉快的传值我要怎么弄账号注册啊

    3、所以在当前的版本休想找回账号密码了

      1 import javax.swing.*;
      2 import java.awt.*;
      3 import java.awt.event.*;
      4 import java.awt.image.BufferedImage;
      5 import java.util.Random;
      6 import java.util.Scanner;
      7 public class Login
      8 {
      9     
     10     public static void main(String[] args)
     11     {    
     12         final String name="ccr",pass="123";
     13         // 创建 JFrame 实例
     14         JFrame frame = new JFrame("请登录");
     15         // Setting the width and height of frame
     16         frame.setSize(350, 230);
     17         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     18 
     19         //创建面板
     20         JPanel panel = new JPanel();    
     21         // 添加面板
     22         frame.add(panel);
     23         panel.setLayout(null);
     24 
     25         // 用户名
     26         JLabel userLabel = new JLabel("登录名");   
     27         userLabel.setBounds(10,20,80,25);
     28         panel.add(userLabel);
     29 
     30         //输入用户名
     31         final JTextField userText = new JTextField(20);
     32         userText.setBounds(100,20,165,25);
     33         panel.add(userText);
     34 
     35         // 输入密码
     36         JLabel passwordLabel = new JLabel("密码");
     37         passwordLabel.setBounds(10,50,80,25);
     38         panel.add(passwordLabel);
     39 
     40 
     41          //输入密码,输入的信息会以点号代替,用于包含密码的安全性
     42 
     43         final JPasswordField passwordText = new JPasswordField(20);
     44         passwordText.setBounds(100,50,165,25);
     45         panel.add(passwordText);
     46         
     47         //文本域-验证码答案  实在不知道这个答案要怎么传了,出此下策
     48         final JTextField verifyTextA = new JTextField(20);
     49         verifyTextA.setBounds(20,80,80,25);
     50         verifyTextA.setVisible(false);
     51         panel.add(verifyTextA);
     52         
     53         //文本域-验证码
     54         final JTextField verifyText = new JTextField(20);
     55         verifyText.setBounds(100,80,80,25);
     56         panel.add(verifyText);
     57      
     58         //按钮-重新生成验证码
     59         JButton Button3=new JButton("刷新");
     60         Button3.setBounds(270, 80, 60, 25);
     61         JLabel lbl = new JLabel();
     62         String v=RandString();
     63         verifyTextA.setText(v);
     64          lbl.setIcon(new ImageIcon(CreateVerify(v)));//"验证"
     65          lbl.setBounds(190,80 , 70, 25);
     66         Button3.addActionListener(new ActionListener()
     67         {
     68             
     69             @Override
     70             public void actionPerformed(ActionEvent arg0)
     71             {
     72                 String v=RandString();
     73                 verifyTextA.setText(v);
     74                  lbl.setIcon(new ImageIcon(CreateVerify(v)));
     75                  lbl.setBounds(190,80 , 70, 25);
     76                  
     77             }
     78         });
     79         panel.add(lbl);
     80         panel.add(Button3);
     81         JButton Button1 = new JButton("登录");
     82         Button1.setBounds(100, 115, 150, 25);
     83         Button1.addActionListener(new ActionListener()
     84         {
     85             
     86             @Override
     87             public void actionPerformed(ActionEvent arg0)
     88             {
     89                 // 登录
     90                 if(verifyText.getText().equals(verifyTextA.getText()))
     91                 {
     92                     if(userText.getText().equals(name))
     93                     {
     94                         if(String.valueOf(passwordText.getPassword()).equals(pass))
     95                         {
     96                             JOptionPane.showMessageDialog(null,"登陆成功");
     97                         }
     98                         else
     99                         {
    100                             JOptionPane.showMessageDialog(null,"密码错误");
    101                         }
    102                     }
    103                     else if(!(userText.getText().equals("用户名/邮箱")||userText.getText().isEmpty()))
    104                     {
    105                         JOptionPane.showMessageDialog(null,"账号不存在");
    106                     }
    107                 }
    108                 else
    109                 {
    110                     JOptionPane.showMessageDialog(null,"验证码错误");
    111                 }
    112             }
    113         });
    114         panel.add(Button1);
    115         JButton Button2 = new JButton("快速注册");
    116         Button2.setBounds(100, 150, 150, 25);
    117         Button2.addActionListener(new ActionListener()
    118         {
    119             
    120             @Override
    121             public void actionPerformed(ActionEvent arg0)
    122             {
    123                 JOptionPane.showMessageDialog(null,"账号:ccr,密码:123,假装注册");
    124                 
    125             }
    126         });
    127         panel.add(Button2);
    128         
    129         userText.addFocusListener(new MyFocusListener("用户名/邮箱",userText));
    130         // 设置界面可见
    131         frame.setVisible(true);
    132         
    133     }
    134     
    135     public static String RandString()
    136     {
    137         String v=new String();
    138         v="";
    139         //随即及生成验证码
    140         for(int i = 0 ; i < 4 ; i ++)
    141         {
    142             //生成一个97~122的int型的整数
    143             int intVal = (int)(Math.random() * 26 + 97);
    144             //将intValue强制转换为char后连接到result后面
    145             v = v + (char)intVal;
    146         }
    147         return v;
    148     }
    149     //生成验证码
    150     public static BufferedImage CreateVerify(String v)
    151     {
    152         // 创建图像缓冲区
    153         BufferedImage img = new BufferedImage(70, 25,
    154         BufferedImage.TYPE_INT_RGB);
    155         // 获取图像上下文(画笔)
    156         Graphics g = img.getGraphics();
    157         // 设定图像背景色,填充背景矩形
    158         g.setColor(getRandomColor(200, 255));
    159         g.fillRect(0, 0, 70, 25);
    160         // 画边框
    161         g.setColor(Color.BLACK);
    162         g.setFont(new Font("楷体", Font.HANGING_BASELINE, 24));
    163         g.drawRect(0, 0, 69, 24);
    164         Random rand = new Random();
    165         for (int i = 0; i < v.length(); i++) {  
    166             // 单个字符绘制宽度
    167             int width = 18;
    168             // 当前字符绘制原点
    169             int x = width * i;
    170             int y = 25 / 2 + rand.nextInt(25 / 3);
    171             /* 将该字符画到图像中 */
    172             drawString(g, x, y,v.substring(i, i+1));
    173         }
    174         
    175         return img;
    176     }
    177     //画一个字符
    178     private static void drawString(Graphics g, int width, int height, String str) {
    179         Random rand = new Random();
    180         // 随机生成字符旋转角度(-30~30度)
    181         int degree = rand.nextInt(60);
    182         if (degree > 30)
    183             degree = 30 - degree;
    184         // 设置字体颜色
    185         g.setColor(getRandomColor(0, 80));
    186         // 转换 Graphics2D
    187         Graphics2D g2 = (Graphics2D) g.create();
    188         // 平移原点到图形环境的中心
    189         g2.translate(width + rand.nextInt(5), height + rand.nextInt(5));
    190         // 旋转文本
    191         g2.rotate(degree * Math.PI / 180);
    192         // 画文本
    193         g2.drawString(str, 0, 0);
    194     }
    195     //随机颜色
    196      private static Color getRandomColor(int minimum, int maximum) 
    197      {
    198             if (minimum > maximum) {
    199                 int tmp = minimum;
    200                 minimum = maximum;
    201                 maximum = tmp;
    202             }
    203             if (maximum > 255)
    204                 maximum = 255;
    205             if (minimum < 0)
    206                 minimum = 0;
    207 
    208             int r = minimum + (int) (Math.random() * (maximum - minimum));
    209             int g = minimum + (int) (Math.random() * (maximum - minimum));
    210             int b = minimum + (int) (Math.random() * (maximum - minimum));
    211 
    212             return new Color(r, g, b);
    213         }
    214 }
    215 class MyFocusListener implements FocusListener {
    216     String info;
    217     JTextField jtf;
    218     public MyFocusListener(String info, JTextField jtf) {
    219         this.info = info;
    220         this.jtf = jtf;
    221     }
    222     @Override
    223     public void focusGained(FocusEvent e) {//获得焦点的时候,清空提示文字
    224         String temp = jtf.getText();
    225         if(temp.equals(info)){
    226             jtf.setText("");
    227         }
    228     }
    229     @Override
    230     public void focusLost(FocusEvent e) {//失去焦点的时候,判断如果为空,就显示提示文字
    231         String temp = jtf.getText();
    232         if(temp.equals("")){
    233             jtf.setText(info);
    234         }
    235     }
    236 }
    Login
  • 相关阅读:
    SQL删除重复记录
    C#分页插件 Webdiyer
    B/S系统操作日志设计思路
    VS2010自动添加版权信息以及更改默认的jquery库
    Gridview中添加CheckBox全选
    利用log4net记录操作日志
    如何在定义游标的时候使用动态sql语句
    java 获取指定月份第一天和最后一天
    java 判断星期几
    SSH登陆错误"WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! "
  • 原文地址:https://www.cnblogs.com/CCRNRT/p/9752317.html
Copyright © 2020-2023  润新知