Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

login password

807597Oct 11 2005 — edited Oct 13 2005
Can any one tell me why my program is not working right?

What is supposed to happen is just logining into a computer. you type in the user name and password, and the program compares the two and if they match then it allows the use to access the computer. This one only shows pass if the user name and password match the users ID and failed if entered incorrectly. Right now it always says fail. If I write the code to get text (example "userNameField.getText()" )from the fields it gives me a "ava uses or overrides a deprecated API" warning but still doesn't wotk. Here is my code.
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 
 public class Authentication extends JFrame{
 	private JLabel userNameLabel, passwordLabel, welcomeLabel;
 	private JTextField userNameField, welcomeField;
 	private JPasswordField passwordField;
 	
 	public Authentication()
 	{
 		super( "Security and Authentication" );
 		
 		Container container = getContentPane();
 		container.setLayout( new FlowLayout() );
 		
 		//user name label and text field
 		userNameLabel = new JLabel( "Enter username: ");
 		container.add( userNameLabel );
 		userNameField = new JTextField( 15 );
 		container.add( userNameField );
		
		//password label and text field
 		passwordLabel = new JLabel( "Enter Password: ");
 		container.add( passwordLabel );
 		passwordField = new JPasswordField( 15 );
 		container.add( passwordField );
 		
 		welcomeLabel = new JLabel( "Authentication test: " );
 		container.add( welcomeLabel );
 		welcomeField = new JTextField( 20 );
 		welcomeField.setEditable ( false );
 		container.add( welcomeField );
 		
 		//event handlers
 		AuthenticationHandler handler = new AuthenticationHandler();
 		userNameField.addActionListener( handler );
 		welcomeField.addActionListener( handler );
 		passwordField.addActionListener( handler );
 		
 		setSize( 380, 200 );
 		setVisible( true );
 	
 	}// end constructorAuthentication
 	
 	public static void main( String[] args )
 	{
 		Authentication application = new Authentication();
 		application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
 		
 	}// end method main
 	
 	
 	// private inner class event handler
 	private class AuthenticationHandler implements ActionListener{
 		
 		public void actionPerformed( ActionEvent event )
 		{
 		
 			String message = "";
 			String un = "me";
 			int pw = 123;
  	 		
 			if (event.getSource() == userNameField && 
 				event.getSource() == passwordField){
 				if( userNameField == un && 
 					passwordField == pw ){
 					message = "Passed";	
 					}
 				else
 					message = "Failed";
 		 	}// end while loop
 				
 			welcomeField.setText( message ); 
 		
 		}// end method actionPreformed
 			
 	}// end inner class
 	
 }// end class Authentication
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 10 2005
Added on Oct 11 2005
5 comments
123 views