mercredi 7 décembre 2016

Problems with if statement, making a keep safe application

I want to make a good "Keep Safe" Programm. I have 2 problems with panel 3. The create button is not on the line start even tho I've put it on line start.

Then I've got a Problem with the action for that Button... The If statement:

else {if(Password != Password1)

wont work properly it works like i put different Passwords it says password aren't identical, i put the same Passwords in its says still the same. Please give me some suggestions to hot wo store password and username better!Also here is the code:

 package button;


 public class Bank {


private static String InWichUsername;
private static String InWichPassword;
private static ArrayList <String> Usernames = new ArrayList<String>(10);
private static ArrayList <String> Passwords = new ArrayList<String>(10);
private static boolean InSystem;



public static String getInWichUsername (){

    return InWichUsername;

}
public static String getInWichPassword (){
    return InWichPassword;

}








public static void main(String[] args) {

    window();
}

public static void window(){
    // insert frame objects
    JFrame frame = new JFrame("KeepSafe ");
    // add  options to it
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,300 );
    frame.setResizable (false);
    frame.setLayout(new BorderLayout());
    ImageIcon img2 = new ImageIcon("C:\\users\\Dominik Gyarmati\\Desktop\\Programming\\Programming Pictures\\Icon4Programm.png ");
    frame.setIconImage(img2.getImage());






    //Panel


    JPanel panel = new JPanel(new GridBagLayout());
    panel.setBackground(Color.BLACK);
    frame.add(panel);
    panel.setLayout(new GridBagLayout());
    GridBagConstraints gbc2 = new GridBagConstraints();
    gbc2.anchor = GridBagConstraints.FIRST_LINE_START;

    //Create Account btn
    JButton btn = new JButton("Create an Account?");
    gbc2.gridx = 2;
    gbc2.gridy = 7;
    gbc2.gridwidth = 1;
    gbc2.gridheight = 1;
    gbc2.fill = GridBagConstraints.HORIZONTAL;
    gbc2.insets = new Insets(5,5,5,5);
    btn.setBackground(Color.BLACK);
    btn.setForeground(Color.MAGENTA);
    btn.setBorder(null);

    panel.add(btn ,gbc2);
    //Login btn
    JButton btn2 = new JButton("Login");
    gbc2.gridx = 0;
    gbc2.gridy = 7;
    gbc2.gridwidth = 1;
    gbc2.gridheight = 1;
    btn2.setBackground(Color.BLACK);
    btn2.setBorder(null);
    btn2.setForeground(Color.MAGENTA);
    gbc2.insets = new Insets(5,5,5,5);
    panel.add(btn2,gbc2);
    JLabel lb1 = new JLabel("Username: ");
    gbc2.gridx = 0;
    gbc2.gridy = 2;
    gbc2.gridwidth = 1;
    gbc2.gridheight = 1;
    lb1.setForeground(Color.MAGENTA);

    gbc2.insets = new Insets(5,5,5,5);
    panel.add(lb1,gbc2);

    JTextField in = new JTextField();
    gbc2.gridx = 0;
    gbc2.gridy = 3;
    gbc2.gridwidth = 4;
    gbc2.gridheight = 1;
    in.setBackground(Color.BLACK);
    in.setForeground(Color.WHITE);
    gbc2.fill = GridBagConstraints.HORIZONTAL;
    in.setCaretColor(Color.WHITE);

    gbc2.insets = new Insets(5,5,5,5);
    panel.add(in,gbc2);

    JLabel lb2 = new JLabel("Password: ");
    gbc2.gridx = 0;
    gbc2.gridy = 4;
    gbc2.gridwidth = 1;
    gbc2.gridheight = 1;
    lb2.setForeground(Color.MAGENTA);
    gbc2.insets = new Insets(5,5,5,5);
    panel.add(lb2,gbc2);

    JTextField pw = new JPasswordField();
    gbc2.gridx = 0;
    gbc2.gridy = 5;
    gbc2.gridwidth = 4;
    gbc2.gridheight = 1;
    gbc2.fill = GridBagConstraints.HORIZONTAL;
    pw.setCaretColor(Color.WHITE);
    pw.setBackground(Color.BLACK);
    pw.setForeground(Color.WHITE);

    gbc2.insets = new Insets(5,5,5,5);
    panel.add(pw,gbc2);

    JCheckBox CheckBox = new JCheckBox();
    gbc2.gridx = 0;
    gbc2.gridy = 6;
    gbc2.gridwidth = 2;
    gbc2.gridheight = 1;
    gbc2.insets = new Insets(5,5,5,5);
    gbc2.fill = GridBagConstraints.HORIZONTAL;
    panel.add(CheckBox,gbc2);
    CheckBox.setBorder(null);
    CheckBox.setBackground(Color.BLACK);
    CheckBox.setForeground(Color.MAGENTA);

    CheckBox.setText("Remember me");





    //Panel2
    JPanel panel2 = new JPanel(new GridBagLayout());
    JButton btn4 = new JButton("Remove Account");
    GridBagConstraints gbc = new GridBagConstraints();
    panel2.setBackground(Color.MAGENTA);

    JButton btn7 = new JButton ("Does Nothing for now");
    gbc.gridx = 1;
    gbc.gridy = 1;
    panel2.add(btn7, gbc);
    JLabel label = new JLabel ();
    JButton btn3 = new JButton("Loggout ");
    gbc.gridx = 2;
    gbc.gridy = 2;

    panel2.add(btn3);
    panel2  .add(btn4);
    label.setIcon(new ImageIcon("C:\\users\\Dominik Gyarmati\\Desktop\\Programming\\Programming Pictures\\Untitled.png "));
    panel2.add(label);




    //Panel3
    JPanel panel3 = new JPanel(new GridBagLayout());
    GridBagConstraints gbc3 = new GridBagConstraints();
    gbc3.anchor = GridBagConstraints.FIRST_LINE_START;
    panel3.setBackground(Color.BLACK);
    JLabel lb = new JLabel("Name: ");
    gbc3.gridx = 0;
    gbc3.gridy = 0;
    gbc3.gridheight = 1;
    gbc3.gridwidth = 1;
    lb.setForeground(Color.MAGENTA);
    gbc3.insets = new Insets(5,5,5,5);
    panel3.add(lb, gbc3);
    JLabel lb3 = new JLabel("Last Name: ");
    gbc3.gridx = 1;
    gbc3.gridy = 0;
    gbc3.gridheight = 1;
    gbc3.gridwidth = 1;
    gbc3.weightx = 1.0;
    lb3.setForeground(Color.MAGENTA);
    gbc3.insets = new Insets(5,5,5,5);
    panel3.add(lb3, gbc3);

    JTextField tx = new JTextField();
    gbc3.gridx = 1;
    gbc3.gridy = 1;
    gbc3.gridheight = 1;
    gbc3.gridwidth = 1;
    tx.setBackground(Color.BLACK);
    tx.setCaretColor(Color.WHITE);
    tx.setForeground(Color.WHITE);
    tx.setBackground(Color.BLACK);
    gbc3.insets = new Insets(5,5,5,5);
    gbc3.fill = GridBagConstraints.HORIZONTAL;;

    panel3.add(tx, gbc3);

    JTextField tx2 = new JTextField();
    gbc3.gridx = 0;
    gbc3.gridy = 1;
    gbc3.gridheight = 1;
    gbc3.gridwidth = 1;
    tx2.setBackground(Color.BLACK);
    tx2.setCaretColor(Color.WHITE);
    tx2.setForeground(Color.WHITE);
    tx2.setBackground(Color.BLACK);
    gbc3.insets = new Insets(5,5,5,5);
    gbc3.fill = GridBagConstraints.HORIZONTAL;;
    panel3.add(tx2, gbc3);


    JButton f = new JButton("Create Account");
    gbc3.gridx = 0;
    gbc3.gridy = 6;
    gbc3.gridwidth = 1;
    gbc3.gridheight = 1;
    gbc3.weightx = 1.0;
    gbc3.anchor = GridBagConstraints.CENTER;
    f.setBorder(null);
    gbc3.insets = new Insets(5,5,5,5);
    f.setBackground(Color.BLACK);
    f.setForeground(Color.MAGENTA);
    panel3.add(f,gbc3);
    gbc3.anchor = GridBagConstraints.FIRST_LINE_START;
    JLabel a = new JLabel("Username: ");
    gbc3.gridx = 0;
    gbc3.gridy = 2;
    gbc3.gridheight = 1;
    gbc3.gridwidth = 1;
    gbc3.insets = new Insets(5,5,5,5);
    a.setForeground(Color.MAGENTA);
    gbc3.insets = new Insets(6,6,6,6);
    panel3.add(a, gbc3);
    JTextField b = new JTextField();
    gbc3.gridx = 0;
    gbc3.gridy = 3;
    gbc3.gridheight = 1;
    gbc3.gridwidth = 2;
    gbc3.fill = 2;
    gbc3.insets = new Insets(5,5,5,5);
    b.setBackground(Color.BLACK);
    b.setCaretColor(Color.WHITE);
    b.setForeground(Color.WHITE);
    b.setBackground(Color.BLACK);
    panel3.add(b, gbc3);
    JLabel jlabel = new JLabel("Confirm Password");
    gbc3.gridx = 1;
    gbc3.gridy = 4;
    gbc3.gridheight = 1;
    gbc3.gridwidth = 1;
    gbc3.weightx = 0.3;
    jlabel.setForeground(Color.MAGENTA);
    panel3.add(jlabel,gbc3);
    JTextField tx3 = new JPasswordField();
    gbc3.gridx = 1;
    gbc3.gridy = 5;
    gbc3.gridheight = 1;
    gbc3.gridwidth = 1;
    gbc3.weightx = 0.3;
    gbc3.insets = new Insets(5,5,5,5);
    gbc.fill = GridBagConstraints.HORIZONTAL;;
    tx3.setBackground(Color.BLACK);
    tx3.setCaretColor(Color.WHITE);
    tx3.setForeground(Color.WHITE);

    panel3.add(tx3,gbc3);
    JLabel c = new JLabel("Password : ");
    gbc3.gridx = 0;
    gbc3.gridy = 4;
    gbc3.gridheight = 1;
    gbc3.gridwidth = 3;
    gbc3.insets = new Insets(5,5,5,5);
    c.setForeground(Color.MAGENTA);
    panel3.add(c, gbc3);
    JTextField d = new JPasswordField();
    gbc3.gridx = 0;
    gbc3.gridy = 5;
    gbc3.gridheight = 1;
    gbc3.gridwidth = 1;
    gbc3.insets = new Insets(5,5,5,5);
    gbc.fill = GridBagConstraints.HORIZONTAL;;
    d.setBackground(Color.BLACK);
    d.setCaretColor(Color.WHITE);
    d.setForeground(Color.WHITE);
    panel3.add(d, gbc3);



    //Panel4






    //Panel 2 





    // Create Account
    f.addActionListener(new ActionListener(){


        public void actionPerformed(ActionEvent e) {
            String Username;
            String Password;
            String Password1;

            Password1 = tx3.getText();
            Password = d.getText();
            Username = b.getText();


            if( Usernames.contains(Username) || Passwords.contains(Password)){
                JOptionPane.showMessageDialog(null, "Choose other username or Password !");


            }
            else {if(Password != Password1){
                JOptionPane.showMessageDialog(null, " Password can not be identical !");

            }
            else if (Password == Password1){
            Usernames.add(b.getText());
            Passwords.add(d.getText());
            InSystem = true;
            frame.remove(panel3);
            frame.add(panel2);
            frame.setSize(1000, 500);
            frame.repaint();
            frame.revalidate();
            JOptionPane.showMessageDialog(null, "You have Sucsessfully created your Account");
            }


        }

    }
    }


    );
    // Create  new Account button  to Create Account panel 
    btn.addActionListener(new ActionListener(){


        public void actionPerformed(ActionEvent e) {
            frame.remove(panel);
            frame.add(panel3);
            frame.setSize(400, 300);
            frame.repaint();
            frame.revalidate();

    }


    });
    //Logout 
    btn3.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            if(InSystem == true)
            {
                InSystem = false;

                frame.remove(panel2);
                frame.add(panel);
                frame.setSize(400, 300);
                frame.repaint();
                frame.revalidate();



            }

    }


    });
    //Login to your acccout
    btn2.addActionListener(new ActionListener(){

        @Override
        public  void actionPerformed(ActionEvent e) {
        String Username;
        String Password;
        Password = in.getText();
        Username = pw.getText();

        if (InSystem = false)
        {

        }
        else if (Usernames.contains(Username) && Passwords.contains(Password) && CheckBox.isSelected()== true) {


                InSystem = true;
                InWichUsername = in.getText();
                InWichPassword = pw.getText();

                frame.remove(panel);
                frame.add(panel2);
                frame.setSize(1000, 500);
                frame.repaint();
                frame.revalidate();

            }
        else if 
        (Usernames.contains(Username) && Passwords.contains(Password)) {


            InSystem = true;
            InWichUsername = in.getText();
            InWichPassword = pw.getText();
            in.setText(null);
            pw.setText(null);
            frame.remove(panel);
            frame.add(panel2);
            frame.setSize(1000, 500);
            frame.repaint();
            frame.revalidate();
        }
            else
            { 
                JOptionPane.showMessageDialog(null, "Username or Password is wrong");
            }


        }
    });
    // remove account
    btn4.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            if(InSystem == true )
            {
                Usernames.remove(getInWichUsername());
                Passwords.remove(getInWichPassword());
                InWichUsername = null;
                InWichPassword = null;


                InSystem = false;
                frame.remove(panel2);
                frame.add(panel);
                frame.setSize(400, 300);
                frame.repaint();
                frame.revalidate();
                JOptionPane.showMessageDialog(null, "You have sucsessfully deleted your Account");
            }
            else
            {

                JOptionPane.showMessageDialog(null, "You have to login in order to Remove your account ");

            }
    }
    });

    }
}

Aucun commentaire:

Enregistrer un commentaire