java - 用户验证密码后如何启动java小程序?

标签 java password-protection

我正在尝试连接两个简单的java程序,一个密码登录程序,然后打开第二个程序(一个非常简单的PrintWriter程序)。

我是个 super 菜鸟,所以尝试将辅助程序添加到密码程序中。显然这不起作用。我看到很多关于创建密码程序的条目,以及一些关于使用 .exec 运行外部应用程序的条目。我想我想做的是嵌入一个在用户登录后运行的程序。

 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();
       }
      }

这是第二个 PrintWriter 程序:

 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();
     }
   }

最佳答案

您可以简单地在成功案例中的 try/catch block 中添加 Philosophers.main 调用,因为 Philosophers.main 可能会抛出 IOException,例如:

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

关于java - 用户验证密码后如何启动java小程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55922605/

相关文章:

java - 在 Web 应用程序中隐藏非常敏感的凭据的正确方法

c# - 如何使用c#打开受密码保护的pdf

java - 选择位置图像不会显示在 ImageView 上

java - Android dagger2 使用 Factory 将 Intent 注入(inject) viewModel

java - 安卓工作室 : Error:Execution failed for task

php - 使用 PHP 打开和创建受密码保护的 zip 文件

c# - 完整性检查 : Salt and hashed passwords

c# - 如何正确保存密码

java - 将 ObservableList 和整数保存/加载为 XML

java - android 和 java 中的 SQLite 数据库