#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Help with password creation


    Hi all, I am have the following code to make the user enter a password

    package Coursework;

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;

    public class pass {

    private static String password = "password";

    public static void main(String[] args) {
    new pass();
    }
    JFrame frame;
    public pass() {
    frame = new JFrame("Password");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 100);

    JLabel label = new JLabel("Enter password");
    JPanel panel = new JPanel();
    frame.add(panel);

    JPasswordField pass = new JPasswordField(10);
    pass.setEchoChar('*');
    pass.addActionListener(this);
    panel.add(label, BorderLayout.WEST);
    panel.add(pass, BorderLayout.EAST);

    frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
    JPasswordField input = (JPasswordField) e.getSource();
    char[] pass = input.getPassword();
    String p = new String(pass);

    if (p.equals(password)) {
    JOptionPane.showMessageDialog(null, "Welcome");
    new UpdateStock();
    frame.dispose();
    } else {
    JOptionPane.showMessageDialog(null, "Please re-enter your password");
    }
    }
    }


    At line 31 the this is red underlined as an error. Does anyone know what is causing this as I cannot figure it out

    Thanks
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    In order to have the current object be an ActionListener, you'll need to implement the ActionListener interface and implement its method.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    47
    Rep Power
    0
    Are You Trying to do this ??

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    import javax.swing.JApplet;
    
    public class GraphGenerator extends JApplet implements ActionListener  {
    	Thread t;boolean zeta=true;
    	int inputfinal=0;
    	Label lab1;
    	TextField text1;
    	Button okButton;
    	GridBagLayout gg=new GridBagLayout();
    	BorderLayout bb=new BorderLayout();
    	public void init(){
    		this.setSize(new Dimension(5000,5000));
    		setLayout(gg);
    		//this.setLocation(200,200);
    		 
    		 
    lab1 = new Label("Enter a number");
    add(lab1);
    text1 = new TextField(20);
    add(text1);text1.addActionListener(this);
    okButton = new Button("Update");okButton.setSize(new Dimension(20,20));
    add(okButton);okButton.addActionListener(this);
    
    	}
    	 
    	public void paint(Graphics g){
    		super.paint(g);
    		//g.setColor(Color.WHITE);
    		g.drawString("You typed: "+text1.getText(),300,100);
    	  
    	  for(int k=0;k<inputfinal;k++){
    		  try {
    			Thread.sleep(1000/20);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		  int col=(int)Math.random()*256;
    		  g.setColor(Color.RED);
    		g.drawRect(50,50,40, (k));
    		 g.setColor(Color.WHITE);
    		 
    		 g.drawRect(50-1,50-1,40-1, (k-1));
    		}
    	}
    	public void actionPerformed(ActionEvent e){
    		//if (e.getSource() == okButton){
    			repaint();
    			String input = text1.getText();
    			inputfinal = Integer.parseInt(input);repaint();
    			 //}
    	}
     
    		
    		
    	}
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by bullet
    In order to have the current object be an ActionListener, you'll need to implement the ActionListener interface and implement its method.
    Hi there. Based on the code I have provided, would you be able to add what you have mentioned in as I have hit a brick wall with this and my mind is frazzled having been doing programming at uni all day

    Your help would be gratefully accepted!

    Many thanks in advanced

IMN logo majestic logo threadwatch logo seochat tools logo