简体   繁体   中英

How to launch a java applet program after user validates password?

I'm trying to connect two simple java programs, a password login program that then opens a second program (a VERY simple PrintWriter program).

I'm a mega noob, so tried just tacking the secondary program into the password program. Obviously that didn't work. I see lots of entries about creating password programs, and a few about using .exec to run external applications. I guess what I'm looking to do is to embed a program that runs after the user has logged in.

 import java.awt.*;
 import java.awt.event.*;
 import java.applet.*;

 public class PasswordApplet extends Applet implements ActionListener
 {
//Declaring variables
  String id, password;
  String[] validID = { "id1", "id2"};
  String[] validPassword = { "password1", "password2"};
  boolean success;

//Create components for applet
Label headerLabel = new Label("Please type your ID and Password");
Label idLabel = new Label("ID: ");
    TextField idField = new TextField(8);
Label passwordLabel = new Label("Password: ");
    TextField passwordField = new TextField(8);
Button loginButton = new Button("Login");

public void init()
{
    //set color, layout, and add components
    setBackground(Color.orange);
    setLayout(new FlowLayout(FlowLayout.LEFT, 50, 30));
    add(headerLabel);
    add(idLabel);
        add(idField);
        idField.requestFocus();
    add(passwordLabel);
        add(passwordField);
        passwordField.setEchoChar('*');
    add(loginButton);
        loginButton.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
    success = false;
    //Sequential search
    int i = 0;
        while ( i<validID.length)
        {
        if(idField.getText().compareTo(validID[i]) == 0)
            {
                if (passwordField.getText().compareTo(validPassword[i]) == 0)
                {
                    success = true;
                }
            }
            i = i + 1;
        }
    if (success == true)
        {
            headerLabel.setText("Login successful");
        }

        else
        {
            headerLabel.setText("Unsuccessful. Try Again");
            idField.setText(" ");
            passwordField.setText(" ");
            idField.requestFocus();
        }

        repaint();
       }
      }

And here's the second PrintWriter program:

 import java.io.*;

 public class Philosophers
 {
public static void main(String[] args) throws IOException
{
    //Declare a PrintWriter variable named myFile and open a file 
            named philosophers.rtf.
    PrintWriter myFile = new PrintWriter("philosophers.rtf");

    //Write the names of 3 philosophers to the file
    myFile.println("John Locke");
    myFile.println("David Hume");
    myFile.println("Edmund Burke");

    //Close the file
    myFile.close();
     }
   }

You could simply add the Philosophers.main call in your success case , in a try/catch block as Philosophers.main may throw an IOException eg :

if (success == true) {
      headerLabel.setText("Login successful");
      try {
           Philosophers.main(null);
      } catch (IOException ex){ex.printStackTrace();}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM