java启动密码,java - 用户验证密码后如何启动java applet程序? - SO中文参考 - www.soinside.com...
我正在嘗試連接兩個簡單的java程序,一個密碼登錄程序,然后打開第二個程序(一個非常簡單的PrintWriter程序)。
我是一個大型菜鳥,所以嘗試將二級程序添加到密碼程序中。顯然這沒用。我看到很多關于創建密碼程序的條目,以及一些關于使用.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
{
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();
}
}
總結
以上是生活随笔為你收集整理的java启动密码,java - 用户验证密码后如何启动java applet程序? - SO中文参考 - www.soinside.com...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java课程设计仓库管理系统_java课
- 下一篇: 一个人成为废材的9大原因