Results 1 to 3 of 3

Thread: [RESOLVED] java textfield password thing.

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    2

    Resolved [RESOLVED] java textfield password thing.

    ellow,

    I want to make a code where you have 3 buttons(1,2,3) and you have to press the buttons in a correct order to open the vault. and the code you entered is shown in a textfield the code is 3321. however i am only able to get one letter every time on the screen now i know why that is and that what i did is wrong, however i do not know the correct way...obviously.
    so i was wondering if someone is kind enough to help me
    here is the code:
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    class MyFrame extends JFrame implements ActionListener
    {
      private JButton b1, b2, b3, b4;
      private JLabel lab1, lab2;
      private JTextField tfUit;
      private final String wachtwoord = "3321";
    
    
              public MyFrame()
                     {
    
                              setLayout(new FlowLayout());
      
                              lab1 = new JLabel  ("raad de geheime code");
                              add(lab1);
    
                              b1 = new JButton("1");
                              add(b1);
                              b1.addActionListener(this);
                              
                              b2 = new JButton("2");
                              add(b2);
                              b2.addActionListener(this);
                              
                              b3 = new JButton("3");
                              add(b3);
                              b3.addActionListener(this);
                              
                               tfUit = new JTextField(8);
                               add(tfUit);
      
                              lab2 = new JLabel("hier komt de uitvoer");
                              add(lab2);
                              
                              b4 = new JButton("clear");
                              add(b4);
                              b4.addActionListener(this);
      
                               setSize(180, 250);
                               setVisible(true);
                               setDefaultCloseOperation(EXIT_ON_CLOSE);
    
                     }
              public void actionPerformed(ActionEvent event) 
              {
                String s = tfUit.getText();
                         if (s.equals(wachtwoord) )
                          {
                            lab2.setText("Yes! geraden");
                          }
                          else
                          {
                            lab2.setText("de kluis blijft dicht");
                          }
    
    
    		if (event.getSource() == b4) 
                    {
    			lab2.setText("hier komt de uitvoer");
    			tfUit.setText("");
    		}
    		if (event.getSource() == b1)
                    {
    			tfUit.getText();
    			tfUit.setText("1");
    		}
    		if (event.getSource() == b2) 
                    {
    			tfUit.getText();
    			tfUit.setText("2");
    		}
    		if (event.getSource() == b3) 
                    {
    			tfUit.getText();
    			tfUit.setText("3");
                    }
    
    
               }
    }

    and this is the main:
    Code:
    public class Main
    {
      public static void main(String [] args)
    {
       MyFrame Frame = new MyFrame();
    
    }
    }

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: java textfield password thing.

    A simple change to append your text. There are other, nicer, ways to accomplish this but using your code you can simply change your events method with the changes below. Also note that you have to move your check for the correct answer to below where the user has clicked the button, otherwise it wont verify their password until after they click a 5th button.

    Code:
    public void actionPerformed(ActionEvent event) {
      if (event.getSource() == b4) {
        lab2.setText("hier komt de uitvoer");
        tfUit.setText("");
      }
    
      if (event.getSource() == b1){
        tfUit.setText(tfUit.getText() + "1");
      }
    
      if (event.getSource() == b2) {
        tfUit.setText(tfUit.getText() + "2");
      }
    
      if (event.getSource() == b3) {
        tfUit.setText(tfUit.getText() + "3");
      }
    		
      String s = tfUit.getText();
    		
      if (s.equals(wachtwoord) ){
        lab2.setText("Yes! geraden");
      }else{
        lab2.setText("de kluis blijft dicht");
      }
    }
    I would also recommend using if/else if for your conditional check since the way it is written now, you will check the condition of every statement whether one of the higher ones results to true or not.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    2

    Re: java textfield password thing.

    Thank you so much, after a while I figured out how to get the whole string in the textfield but yeh saw you had to push one extra button to get the "vault" open. but you fixed that too.
    ^^ so happy now.

    now I feel ashamed that I spend so much time trying to make it work while the solution is so simple...thank god there are people that know better than me ^^

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width