登录验证码及猜数字的图形化界面编程

任务1

题目:

程序设计思想:

先设计出登录界面的图形化界面,设计出按钮,为按钮添加事件监听,再设计出注册的图形化界面,同样也为注册界面的按钮添加事件监听,弹出各种提示框,让用户对应相应操作。

程序流程图如下

程序源代码:

package com.test;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.Random;

import javax.swing.*;
public class Test2 {
    JPanel p=new JPanel(null);
    JFrame a=new JFrame();
    Test2()
    {
        JLabel lbTitle=new JLabel("请登录");
        JLabel lbUserName=new JLabel("登录名: ");
        JTextField txtUserName=new JTextField();
        JLabel lbPassword=new JLabel(" 密码: ");
        JPasswordField txtPassword=new JPasswordField();     //登录界面
        JLabel lbYanzheng=new JLabel("验证码: ");
        JButton loginBtn=new JButton("快速注册");
        JTextField txtYanzheng=new JTextField();
        JButton forgetBtn=new JButton("登录");
        int x;
        x=new Random().nextInt(1000000)+1;                                  //随机数
        String y=String.valueOf(x);
        JButton maBtn=new JButton(y);
        JButton shuaxinBtn=new JButton("刷新");
        lbUserName.setBounds(20, 10, 250, 35);
        txtUserName.setBounds(70, 10, 200, 25);
        lbPassword.setBounds(20, 50, 250, 35);
        txtPassword.setBounds(70, 50, 200, 25);
        lbYanzheng.setBounds(20, 90, 250, 35);
        txtYanzheng.setBounds(70, 90, 130, 25);
        loginBtn.setBounds(220, 150, 100, 35);
        forgetBtn.setBounds(80, 150, 100, 35);
        maBtn.setBounds(200, 90, 80, 25);
        shuaxinBtn.setBounds(290, 90, 70, 25);
        p.add(maBtn);
        p.add(shuaxinBtn);
        p.add(lbTitle);
        p.add(lbUserName);
        p.add(txtUserName);
        p.add(lbPassword);
        p.add(txtPassword);
        p.add(lbYanzheng);
        p.add(txtYanzheng);
        p.add(loginBtn);
        p.add(forgetBtn);
        shuaxinBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                int a;
                a=new Random().nextInt(1000000)+1;
                String b=String.valueOf(a);
                String c=maBtn.getText();
                c=b;
                maBtn.setText(b);
            }
            
        });
        forgetBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                String a=txtUserName.getText();
                String b=txtPassword.getText();
                String c=txtYanzheng.getText();
                String f=maBtn.getText();
                boolean g=true;
                if(a.equals("")||b.equals("")||c.equals(""))
                {
                    JOptionPane.showMessageDialog(null,"用户某项输入为空,请重新输入","提示",JOptionPane.CLOSED_OPTION);
                }
                while(g)
                {
                    if(c.equals(f))
                    {
                        JOptionPane.showConfirmDialog(null, "登陆成功!","登陆结果",JOptionPane.CLOSED_OPTION);
                        break;
                    }
                    else
                    {
                        JOptionPane.showConfirmDialog(null, "登陆失败,请重新输入验证码!","登陆结果",JOptionPane.CLOSED_OPTION);
                        break;
                    }
                }
            }
        });
        loginBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                zhuce();    //为快速注册按钮添加事件监听
            }
        });
        a.getContentPane().add(p);
        a.setTitle("请登录");
        a.setSize(400,300);
        a.setVisible(true);
        a.setLayout(null);
        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        a.validate();
        
    }
    public void zhuce()
    {
        JPanel f=new JPanel(null);
        JFrame a=new JFrame();
        JLabel name=new JLabel("用户名");
        JTextField txtName=new JTextField();
        JLabel telephone=new JLabel("电话");
        JTextField txtTelephone=new JTextField();
        JLabel word=new JLabel("密码");
        JPasswordField txtWord=new JPasswordField();     //注册界面
        JLabel word1=new JLabel("确认密码");
        JPasswordField txtWord1=new JPasswordField();
        JButton queBtn=new JButton("确定");
        JButton buBtn=new JButton("取消"); 
        name.setBounds(20, 10, 250, 35);
        txtName.setBounds(80, 10, 200, 25);
        telephone.setBounds(20, 50, 250, 35);
        txtTelephone.setBounds(80, 50, 200, 25);        //设置位置
        word.setBounds(20, 90, 250, 35);
        txtWord.setBounds(80, 90, 130, 25);
        word1.setBounds(20, 130, 250, 35);
        txtWord1.setBounds(80, 130, 130, 25);
        queBtn.setBounds(50, 170, 70, 25);
        buBtn.setBounds(200, 170, 70, 25);
        queBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                String a=txtWord.getText();
                String b=txtWord1.getText();
                String c=txtName.getText();
                String d=txtTelephone.getText();
                if(c.equals("")||d.equals("")||a.equals("")||b.equals(""))
                {
                    JOptionPane.showConfirmDialog(null, "注册失败,请检查某一项输入是否为空","注册结果",JOptionPane.CLOSED_OPTION);
                }
                else if(a.equals(b))
                    JOptionPane.showConfirmDialog(null, "注册成功!","注册结果",JOptionPane.CLOSED_OPTION);
                else
                    JOptionPane.showConfirmDialog(null, "注册失败,两次密码输入不一致!","注册结果",JOptionPane.CLOSED_OPTION);
                
            }
        });
        buBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JOptionPane.showConfirmDialog(null, "取消注册!","注册结果",JOptionPane.CLOSED_OPTION);
                a.setVisible(false);
            }
        });
        f.add(queBtn);
        f.add(buBtn);
        f.add(name);
        f.add(txtName);
        f.add(word);
        f.add(txtWord);
        f.add(word1);    //布局
        f.add(txtWord1);
        f.add(telephone);
        f.add(txtTelephone);
        a.getContentPane().add(f);
        a.setTitle("请注册");
        a.setSize(400,300);
        a.setVisible(true);
        a.setLayout(null);
        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        a.validate();
    }
    public static void main(String[] args)
    {
        new Test2();
    }
}

 程序实验结果截图如下:

实验总结:

在编程的过程中出现过组件布局混乱和JFrame歧义的情况,不过后来都改正过来了,下次继续努力。

任务2

题目:

程序设计思想:

设置随机数,设置输入框输入数字,判断比较,用while循环,不断输出猜大了或者猜小了的提示框,直到猜对了调出循环,输出猜对了的提示框。

程序流程图:

程序源代码:

package demo;

import java.util.Random;

import javax.swing.JOptionPane;

public class Test3 {

  public static void main(String[] args) {
  // TODO 自动生成的方法存根
  String name=JOptionPane.showInputDialog("请输入1到100的随机数");
  String title="your information";
  int a=Integer.parseInt(name);                                                     //将输入的String型转换为int型
  int x;
  x=new Random().nextInt(100)+1;                                             //设置随机数的范围为1到100
  while(a!=0)
  {
    if(a==x)
    {
      JOptionPane.showConfirmDialog(null, "猜对了!","猜测结果",JOptionPane.CLOSED_OPTION);                                     
      break;                                                           //判断输入的数是否和随机数相等
    }
    else if(a<x)
    {
      JOptionPane.showConfirmDialog(null, "猜小了!","猜测结果",JOptionPane.CLOSED_OPTION);

    }
    else if(a>x)
    {
      JOptionPane.showConfirmDialog(null, "猜大了!","猜测结果",JOptionPane.CLOSED_OPTION);

    }
    name=JOptionPane.showInputDialog("请输入1到100的随机数");
    title="your information";
    a=Integer.parseInt(name);                                                 //输出重新输入对话框
  }

 }

}

程序验证结果截图如下

编译错误分析

 需将输入的字符串转换为int类型,然后才能进行和随机数的比较,或者将随机数转换为String类型也可,需要用到String.valueOf()函数。

 第一次写,变量命名可能有些简单,见谅

原文地址:https://www.cnblogs.com/z12568/p/7631568.html