0

This is the Class with the main method:

import javax.swing.JFrame;

class apples{
    public static void main(String args[]){
        Gui go = new Gui();
        go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        go.setSize(180,200);
        go.setVisible(true);
    }
}

This is the class with the actual code for my program:

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;

public class Gui extends JFrame{

     JButton n1 = new JButton("1");
     JButton n2 = new JButton("2");
     JButton n3 = new JButton("3");
     JButton n4 = new JButton("4");
     JButton n5 = new JButton("5");
     JButton n6 = new JButton("6");
     JButton n7 = new JButton("7");
     JButton n8 = new JButton("8");
     JButton n9 = new JButton("9");

     JTextField username = new JTextField("username");
     JPasswordField password = new JPasswordField("password");

     JButton play;

    public Gui(){
        super("Security Passage");
        setLayout(new FlowLayout());

        add(username);
        add(password);

        add(n1);
        add(n2);
        add(n3);
        add(n4);
        add(n5);
        add(n6);
        add(n7);
        add(n8);
        add(n9);

        Icon dec = new ImageIcon(getClass().getResource("faceb.png"));
        play = new JButton("Login", dec);
        add(play);

        HandlerClass handler = new HandlerClass();
        username.addActionListener(handler);
        password.addActionListener(handler);
        n1.addActionListener(handler);
        n2.addActionListener(handler);
        n3.addActionListener(handler);
        n4.addActionListener(handler);
        n5.addActionListener(handler);
        n6.addActionListener(handler);
        n7.addActionListener(handler);
        n8.addActionListener(handler);
        n9.addActionListener(handler);
        play.addActionListener(handler);
    }

    private class HandlerClass implements ActionListener{

        public void actionPerformed(ActionEvent e){

             String cUser = "";
             String cPass = "";
             int cCode[] = {10,10,10};
             String CodeCheck = String.format("%d%d%d", cCode[0], cCode[1], cCode[2]);

             if(e.getSource() == username){
                 cUser = String.format("%s",e.getActionCommand());
             }else if(e.getSource() == password){
                 cPass = String.format("%s",e.getActionCommand());
             }else if(e.getSource() == n1){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n2){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n3){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n4){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n5){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n6){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n7){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n8){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n9){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == play){
                 if(cPass == "Skyfall" && CodeCheck == "123"){
                     JOptionPane.showMessageDialog(null, cUser + ", you have cracked the security system");
                 }else{
                     JOptionPane.showMessageDialog(null, "No Access Granted" + cUser);
                     JOptionPane.showMessageDialog(null, CodeCheck);
                     JOptionPane.showMessageDialog(null, cPass);
                     JOptionPane.showMessageDialog(null, cUser);
                 }
             }
        }
    }
}

This is a program I wrote on my own using Eclipse, however I do not understand why cUser, cPass and CodeCheck do not change their values when an 'action' occurs (clicking on the button in the window that pops-up). Furthermore I believe that there must be a more efficient way to write this code, I would really appreciate any advice and help on these two issues.

Thank you in advance!

unknown
  • 4,859
  • 10
  • 44
  • 62
Iceman
  • 13
  • 4
  • 2
    possible duplicate [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Reimeus Mar 12 '14 at 19:51
  • `if(cPass == "Skyfall" && CodeCheck == "123"){` See [How Do I compare Strings in Java](http://stackoverflow.com/q/513832/2587435) – Paul Samsotha Mar 12 '14 at 19:51
  • 1
    _"Furthermore I believe that there must be a more efficient way to write this code,"_ - Check out [Code Review](http://codereview.stackexchange.com/) – Paul Samsotha Mar 12 '14 at 20:06

0 Answers0