这是一个我正在忙着制作的小程序,但它没有按应有的方式更新。任何人都可以告诉我哪里出了问题吗?这是我正在学习的类(class)的一部分,因此它必须或多或少保持原样。 请记住,我是 java 的初学者,谢谢大家......
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class PasswordApplet extends Applet implements ActionListener
{
//Declaring variables
String id, password;
boolean success;
String idArray[] = {"Reign", "Test", "Others"};
String passwordArray[] = {"130489", "1234", "1111"};
//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
for (int i = 0; i<idArray.length; i++)
{
if ((idArray[i].compareTo(id)==0)&&(passwordArray[i].compareTo(password)==0))
success=true;
}
if(success=true)
{
headerLabel.setText("Login Successful");
headerLabel.repaint();
}
else
{
headerLabel.setText("Invalid. Try Again");
headerLabel.repaint();
}
repaint();
}
}
最佳答案
在 ActionListener
中比较其值之前,您需要分配 id
字段
id = idField.getText();
否则将抛出NPE
,阻止任何 UI 更新
关于Java 小程序没有按应有的方式重印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23082053/