JavaRanch Home
 
Front Page FAQs Ranchers Categories Recent Changes To Do Upload

Post Real Code   



please suggest me below code out put

// [MyApp?.java]

import java.awt.*;

public class myApp extends java.applet.Applet {

 public String username = "";

 public String password = "";  

 boolean userValid = false;

 public void init() {

   while (!userValid) login();  

   //if you get here, the user name and password are correct

   }

 public void login() {

  MyLogin? login = new MyLogin? (new Frame(""));//Calling another class MyLogin? to create a new Frame.

  requestFocus();//In order to receive keyboard input, you have to call this method.

  if (login.id) {

    username = login.username.getText();// Get the username from MyLogin? class

    password = login.password.getText();// Get the password from MyLogin? class 

    userValid = validateUser(username , password);

    System.out.println(userValid?"valid":"invalid");

    }

  login.dispose();//When the dialog box is closed, dispose is called. It frees all system resources associated with the dialog box window.

  }

 private boolean validateUser(String usr, String pwd) {

   // here you will code some logic to validate the username and password

   // for testing purpose :

   // username = applet  and password = niit

   return (usr.equals("applet") && pwd.equals("niit"));

   }

}

Here is the code for MyLogin?.java file.

// [MyLogin?.java]

import java.awt.*;

import java.awt.event.*;

public class MyLogin? extends Dialog implements ActionListener? {

 boolean id = false;

 Button ok,can;

 TextField? username;

 TextField? password;

 MyLogin?(Frame frame){

  super(frame, "Welcome", true); //Calling super class argument constructor

  setLayout(new FlowLayout?()); //Setting the current layout manager, FlowLayout? is the default layout manager.

  username = new TextField?(15); // Creating a TextField?

  password = new TextField?(15);

  password.setEchoChar('*'); // Set the password echoing character to *

  add(new Label("User :")); //Creating and adding a new Label

  add(username); //adding the component created earlier

  add(new Label("Password :"));

  add(password);

  addOKCancelPanel();

  createFrame();

  pack();

  setVisible(true); // A Frame window will not come into visibility until you call this method.

  }

 void addOKCancelPanel() {

  Panel p = new Panel();

  p.setLayout(new FlowLayout?());

  createButtons( p );

  add( p );

  }

 void createButtons(Panel p) {

  p.add(ok = new Button("OK"));

  ok.addActionListener(this); // Started listening action events for OK button 

  p.add(can = new Button("Cancel"));

  can.addActionListener(this); // Started listening action events for Cancel button 

  }

 void createFrame() {

  Dimension d = getToolkit().getScreenSize();

  setLocation(d.width/4,d.height/3);

  }

 public void actionPerformed(ActionEvent? ae){

  if(ae.getSource() == ok) { // if ok is clicked

    id = true;

    setVisible(false);

    }

  else if(ae.getSource() == can) { // if cancel is clicked

    setVisible(false);

    }

  }

}
Front Page FAQs Ranchers Categories Recent Changes To Do Upload
Last Edited: 19 August 2006 What's Changed?
 
Copyright © 1998-2008 Paul Wheaton | Home | Contact Us | Privacy | Register