Java学习:一个仿ATM机键盘做的密码输入

1.程序运行截图:

Java学习:一个仿ATM机键盘做的密码输入

2.程序实现的功能

1)进入程序时,程序会询问你输入一个密码

2)进入键盘界面后,按下数字0-9可以输入一个字符,按取消按钮可以退出程序,重置按钮可以将上面的JPasswordField变为空,按下确定按钮,提示输入的密码是否正确

3.程序代码

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

public class FrameTest_20150214 
{
    //密码
    public static char[] correctPassword = {'1', '2', '3', '4', '5', '6'};
    
    public static void main(String[] args) 
    {    
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                //输入一个密码,要求密码全部为数字组成
                boolean isNumber = false;
                String inputPassword = "";
                while(!isNumber)
                {
                    inputPassword = JOptionPane.showInputDialog("请输入一个密码");
                    isNumber = inputPassword.matches("[0-9]+");
                }
                
                correctPassword = inputPassword.toCharArray();
                
                //打开窗体
                TestFrame testFrame = new TestFrame(correctPassword);
                testFrame.setVisible(true);
            }
        });
    }
}

//密码输入窗体
class TestFrame extends JFrame
{
    private static final long serialVersionUID = 1L;
    private static final int DEFAULT_WIDTH = 600;
    private static final int DEFAULT_HEIGHT = 400;

    //密码输入框
    JPasswordField psdDisplay;

    //数字键盘容器
    JPanel panel;

    //数字键盘上的按钮
    JButton btnNum1;   //按键1
    JButton btnNum2;   //按键2
    JButton btnNum3;   //按键3
    JButton btnNum4;   //按键4
    JButton btnNum5;   //按键5
    JButton btnNum6;   //按键6
    JButton btnNum7;   //按键7
    JButton btnNum8;   //按键8
    JButton btnNum9;   //按键9
    JButton btnNum0;   //按键0
    JButton btnDot;    //小数点
    JButton btnNum00;  //按键00
    JButton btnOK;     //确认
    JButton btnCancel; //取消
    JButton btnReset;  //重置
    
    //正确的密码
    private char[] correctPassword;
    
    //构造函数
    public TestFrame(char[] password)
    {
        correctPassword = password;
        
        SetTestFrame();
        SetControls();
    }
    
    //设置窗体
    private void SetTestFrame()
    {
        //窗口大小
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        //窗口居中
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(
            (dim.width - getSize().width) / 2, 
            (dim.height - getSize().height) / 2);
        
        //标题
        setTitle("This is a Frame for test!");
        
        //使窗体边界无法改变(也无法进行最大化)
        setResizable(false);

        //关闭窗口时退出程序
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    //设置控件
    private void SetControls()
    {
        setLayout(new BorderLayout());
        
        //密码输入框
        psdDisplay = new JPasswordField();
        psdDisplay.setEchoChar('*');
        psdDisplay.setHorizontalAlignment(JTextField.CENTER);
        psdDisplay.setFont(new Font("Times New Roman", Font.BOLD, 60));
        psdDisplay.setEditable(false);
        add(psdDisplay, BorderLayout.NORTH);
        
        //要绑定的事件
        InsertNumberAction insertNumber = new InsertNumberAction();
        OkCommandAction okCommand = new OkCommandAction();
        CancelCommandAction cancelCommand = new CancelCommandAction();
        ResetCommandAction resetCommand = new ResetCommandAction();
        
        //输入键盘
        panel = new JPanel();
        panel.setLayout(new GridLayout(4, 4));
        
        //数字7
        btnNum7 = new JButton("7");
        btnNum7.setFont(new Font("Andalus", Font.BOLD, 60));
        btnNum7.addActionListener(insertNumber);
        panel.add(btnNum7);

        //数字8
        btnNum8 = new JButton("8");
        btnNum8.setFont(new Font("Andalus", Font.BOLD, 60));
        btnNum8.addActionListener(insertNumber);
        panel.add(btnNum8);

        //数字9
        btnNum9 = new JButton("9");
        btnNum9.setFont(new Font("Andalus", Font.BOLD, 60));
        btnNum9.addActionListener(insertNumber);
        panel.add(btnNum9);
        
        //取消按钮
        btnCancel = new JButton("取消");
        btnCancel.setForeground(Color.RED);
        btnCancel.setFont(new Font("宋体", Font.BOLD, 40));
        btnCancel.addActionListener(cancelCommand);
        panel.add(btnCancel);

        //数字4
        btnNum4 = new JButton("4");
        btnNum4.setFont(new Font("Andalus", Font.BOLD, 60));
        btnNum4.addActionListener(insertNumber);
        panel.add(btnNum4);

        //数字5
        btnNum5 = new JButton("5");
        btnNum5.setFont(new Font("Andalus", Font.BOLD, 60));
        btnNum5.addActionListener(insertNumber);
        panel.add(btnNum5);

        //数字6
        btnNum6 = new JButton("6");
        btnNum6.setFont(new Font("Andalus", Font.BOLD, 60));
        btnNum6.addActionListener(insertNumber);
        panel.add(btnNum6);

        //重置按钮
        btnReset = new JButton("重置");
        btnReset.setForeground(Color.BLUE);
        btnReset.setFont(new Font("宋体", Font.BOLD, 40));
        btnReset.addActionListener(resetCommand);
        panel.add(btnReset);

        //数字1
        btnNum1 = new JButton("1");
        btnNum1.setFont(new Font("Andalus", Font.BOLD, 60));
        btnNum1.addActionListener(insertNumber);
        panel.add(btnNum1);

        //数字2
        btnNum2 = new JButton("2");
        btnNum2.setFont(new Font("Andalus", Font.BOLD, 60));
        btnNum2.addActionListener(insertNumber);
        panel.add(btnNum2);

        //数字3
        btnNum3 = new JButton("3");
        btnNum3.setFont(new Font("Andalus", Font.BOLD, 60));
        btnNum3.addActionListener(insertNumber);
        panel.add(btnNum3);

        //空按钮
        panel.add(new JButton(""));

        //小数点(摆设)
        btnDot = new JButton(".");
        btnDot.setFont(new Font("Andalus", Font.BOLD, 60));
        panel.add(btnDot);

        //数字0
        btnNum0 = new JButton("0");
        btnNum0.setFont(new Font("Andalus", Font.BOLD, 60));
        btnNum0.addActionListener(insertNumber);
        panel.add(btnNum0);

        //数字00(摆设)
        btnNum00 = new JButton("00");
        btnNum00.setFont(new Font("Andalus", Font.BOLD, 60));
        panel.add(btnNum00);

        //确定按钮
        btnOK = new JButton("确定");
        btnOK.setForeground(Color.GREEN);
        btnOK.setFont(new Font("宋体", Font.BOLD, 40));
        btnOK.addActionListener(okCommand);
        panel.add(btnOK);

        add(panel, BorderLayout.CENTER);
    }
    
    //按下数字按钮触发的事件
    private class InsertNumberAction implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            psdDisplay.setText(String.valueOf(psdDisplay.getPassword()) + 
                event.getActionCommand());
        }
    }

    //按下确定按钮触发的事件
    private class OkCommandAction implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            char[] inputPassword = psdDisplay.getPassword(); 
            boolean isMatch = true;
            
            //密码长度需要相同
            if (inputPassword.length != correctPassword.length)
            {
                isMatch = false;
            }

            //密码长度相同的情况下各位要一致
            if (isMatch)
            {
                isMatch = Arrays.equals(inputPassword, correctPassword);
            }
            
            if (isMatch)
            {
                JOptionPane.showMessageDialog(null, "密码正确!", "提示信息", 
                    JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "密码错误", "错误信息", 
                    JOptionPane.ERROR_MESSAGE);
                psdDisplay.setText("");
            }
        }
    }

    //按下取消按钮触发的事件
    private class CancelCommandAction implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            JOptionPane.showMessageDialog(null, "即将退出程序", "提示信息", 
                JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
        }
    }

    //按下重置按钮触发的事件
    private class ResetCommandAction implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            psdDisplay.setText("");
        }
    }
}

END

你可能感兴趣的:(Java学习:一个仿ATM机键盘做的密码输入)