如何在用户验证密码后启动java小程序?
我试图连接两个简单的java程序,一个密码登录程序,然后打开第二个程序(一个非常简单的PrintWriter程序) 我是个超级noob,所以试着把第二个程序加入密码程序。显然,这不起作用。我看到很多关于创建密码程序的条目,还有一些关于使用.exec运行外部应用程序的条目。我想我要做的是嵌入一个在用户登录后运行的程序如何在用户验证密码后启动java小程序?,java,password-protection,Java,Password Protection,我试图连接两个简单的java程序,一个密码登录程序,然后打开第二个程序(一个非常简单的PrintWriter程序) 我是个超级noob,所以试着把第二个程序加入密码程序。显然,这不起作用。我看到很多关于创建密码程序的条目,还有一些关于使用.exec运行外部应用程序的条目。我想我要做的是嵌入一个在用户登录后运行的程序 import java.awt.*; import java.awt.event.*; import java.applet.*; public class Passwor
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();
}
}
在成功案例中,您可以简单地在try/catch块中添加哲学家.main调用,因为哲学家.main可能抛出IOException,例如:
if (success == true) {
headerLabel.setText("Login successful");
try {
Philosophers.main(null);
} catch (IOException ex){ex.printStackTrace();}
if (success == true) {
headerLabel.setText("Login successful");
try {
Philosophers.main(null);
} catch (IOException ex){ex.printStackTrace();}