为我的银行应用程序制作一个ENTER按钮

时间:2014-10-06 16:00:39

标签: java arrays swing

我正在尝试登录屏幕,其中有10个按钮0-10,只接受4位数字。我存储了一些名为PIN0-PIN03的变量,它保存了一个int值。我希望用户输入4位数的引脚,如果它与存储在变量PIN0-PIN03中的值匹配,则会出现一个名称。

我已完成按钮,我只需要帮助这个ENTER按钮

我用于ENTER按钮的代码是:

int pin = 0000;
int PIN0 = 1234; // if user enters this value the program will start executing the if statments.
int PIN1 = 2345;
int PIN2 = 3456;
int PIN3 = 4567;\\

if (pin == PIN0){

System.out.println("Hi Muhannad");
if (pin == PIN0){ // if user enters PIN0 it will display the below message

System.out.println("Hi Muhannad");
}

if( pin == PIN1){
System.out.println("Hi Mo");
}

if( pin == PIN2){
System.out.println("Hi Mahir");
}

if( pin == PIN3){


System.out.println("Hi Gawi");
}

if(pin != PIN0 || pin != PIN1 || pin != PIN2 || pin != PIN3){
System.out.println("Incorrect pin try again!");  // if user enters anything that doesn't match any of the variables values the "Incorrect pin try again" will apear. 
}
我是这样做的吗?

请帮助这是我自己创建的一个项目,用于创建类似于银行应用程序的应用程序。

按钮代码:

private void buttonOneActionPerformed(java.awt.event.ActionEvent evt) {                                          
if(evt.getSource() == buttonOne){
            if(jTextField1.getText().length() < 4){
                if(clearField == 1){
                    jTextField1.setText("0");
                    clearField = 0;
                } else{
                    jTextField1.setText(jTextField1.getText() + "1");
                }
            }
}
}                                         

private void buttonTwoActionPerformed(java.awt.event.ActionEvent evt) {                                          
if(evt.getSource() == buttonTwo){
            if(jTextField1.getText().length() < 4){
                if(clearField == 1){
                    jTextField1.setText("2");
                    clearField = 0;
                } else{
                    jTextField1.setText(jTextField1.getText() + "2");
                }
            }
}
}                                         

private void buttonThreeActionPerformed(java.awt.event.ActionEvent evt) {                                            
if(evt.getSource() == buttonThree){
            if(jTextField1.getText().length() < 4){
                if(clearField == 1){
                    jTextField1.setText("3");
                    clearField = 0;
                } else{
                    jTextField1.setText(jTextField1.getText() + "3");
                }
            }
}
}                                           

private void buttonFourActionPerformed(java.awt.event.ActionEvent evt) {                                           
if(evt.getSource() == buttonFour){
            if(jTextField1.getText().length() < 4){
                if(clearField == 1){
                    jTextField1.setText("4");
                    clearField = 0;
                } else{
                    jTextField1.setText(jTextField1.getText() + "4");
                }
            }
}
}                                          

private void buttonFiveActionPerformed(java.awt.event.ActionEvent evt) {                                           
if(evt.getSource() == buttonFive){
            if(jTextField1.getText().length() < 4){
                if(clearField == 1){
                    jTextField1.setText("5");
                    clearField = 0;
                } else{
                    jTextField1.setText(jTextField1.getText() + "5");
                }
            }
}
}                                          

private void buttonSixActionPerformed(java.awt.event.ActionEvent evt) {                                          
if(evt.getSource() == buttonSix){
            if(jTextField1.getText().length() < 4){
                if(clearField == 1){
                    jTextField1.setText("6");
                    clearField = 0;
                } else{
                    jTextField1.setText(jTextField1.getText() + "6");
                }
            }
}// TODO add your handling code here:
}                                         

private void buttonSevenActionPerformed(java.awt.event.ActionEvent evt) {                                            
if(evt.getSource() == buttonSeven){
            if(jTextField1.getText().length() < 4){
                if(clearField == 1){
                    jTextField1.setText("7");
                    clearField = 0;
                } else{
                    jTextField1.setText(jTextField1.getText() + "7");
                }
            }
}
}                                           

private void buttonEightActionPerformed(java.awt.event.ActionEvent evt) {                                            
if(evt.getSource() == buttonEight){
            if(jTextField1.getText().length() < 4){
                if(clearField == 1){
                    jTextField1.setText("8");
                    clearField = 0;
                } else{
                    jTextField1.setText(jTextField1.getText() + "8");
                }
            }
}
}                                           

private void buttonNineActionPerformed(java.awt.event.ActionEvent evt) {                                           
if(evt.getSource() == buttonNine){
            if(jTextField1.getText().length() < 4){
                if(clearField == 1){
                    jTextField1.setText("9");
                    clearField = 0;
                } else{
                    jTextField1.setText(jTextField1.getText() + "9");
                }
            }
}
}                                          

private void buttonZeroActionPerformed(java.awt.event.ActionEvent evt) {                                           
if(evt.getSource() == buttonZero){
            if(jTextField1.getText().length() < 4){
                if(clearField == 1){
                    jTextField1.setText("0");
                    clearField = 0;
                } else{
                    jTextField1.setText(jTextField1.getText() + "0");
                }
            }
}

1 个答案:

答案 0 :(得分:0)

您没有提供有关代码应如何工作的大量信息,但这是一个示例:

public class Bank extends JFrame {

    static final int PIN0 = 1234;
    static final int PIN1 = 2345;
    static final int PIN2 = 3456;
    static final int PIN3 = 4567;

    JTextField tf = new JTextField();

    public Bank() {

        JPanel buttonsPanel = new JPanel(new GridLayout(4, 3));

        for (int i = 0; i < 10; i++) {
            JButton button = new JButton(String.valueOf(i));
            button.addActionListener(new NumberAction(i));
            buttonsPanel.add(button);
        }

        JButton enterButton = new JButton("Enter");
        enterButton.addActionListener(new EnterAction());

        getContentPane().add(tf, BorderLayout.PAGE_START);
        getContentPane().add(buttonsPanel, BorderLayout.CENTER);
        getContentPane().add(enterButton, BorderLayout.PAGE_END);

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    class NumberAction extends AbstractAction {

        int i;

        NumberAction(int i) {

            this.i = i;
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            if (tf.getText().length() < 4)
                tf.setText(tf.getText() + i);
        }
    }

    class EnterAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {

            switch (Integer.parseInt(tf.getText())) {
                case PIN0:
                    System.out.println("Hi 0");
                    break;
                case PIN1:
                    System.out.println("Hi 1");
                    break;
                case PIN2:
                    System.out.println("Hi 2");
                    break;
                case PIN3:
                    System.out.println("Hi 3");
                    break;
                default:
                    System.out.println("Incorrect pin try again!");
            }

            tf.setText("");
        }
    }

    public static void main(String args[]) {

        new Bank();
    }
}