如何使用数组为密码和ID创建GUI java
应用程序将询问用户ID和密码,以及他们是现有用户还是新用户。如果您是新用户,系统将提示您重新输入详细信息以确认id和密码,然后这些信息将存储在阵列中。 一旦用户单击登录按钮,应用程序将通过ID和密码数组进行搜索以进行验证。(将您的数组设置为最多包含10个用户–假设它是一个小数组,则可以使用线性搜索) 然后,应用程序将显示相应的消息
到目前为止我的代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Log extends JFrame
{
public static void main(String[] args)
{
Log frameTabel = new Log();
}
JLabel title = new JLabel("Please type your ID and Password");
JLabel id = new JLabel("ID:");
JLabel psword = new JLabel("Password:");
JButton blogin = new JButton("Login");
JPanel panel = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);
JRadioButton radNew = new JRadioButton("New", true);
JRadioButton radExisting = new JRadioButton("Existing", false);
ButtonGroup radioGroup1 = new ButtonGroup();
Log()
{
super("Week 9 Question 3");
setSize(300,250);
setLocation(600,250);
panel.setLayout (null);
title.setBounds(40,5,300,20);
id.setBounds(52,40,150,20);
txuser.setBounds(70,40,150,20);
psword.setBounds(7,80,150,20);
pass.setBounds(70,80,150,20);
blogin.setBounds(180,130,80,20);
radNew.setBounds(30, 130, 50, 20);
radExisting.setBounds(90, 130, 90, 20);
panel.add(title);
panel.add(id);
panel.add(psword);
panel.add(blogin);
panel.add(txuser);
panel.add(pass);
add(radNew);
add(radExisting);
radioGroup1.add(radNew);
radioGroup1.add(radExisting);
map.put("test", "12345");
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionlogin();
}
public void actionlogin() {
blogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String puname = txuser.getText();
String ppaswd = new String(pass.getPassword());
JOptionPane.showMessageDialog(null, "Correct Information", "Correct",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "No Password/ID Found on System",
"Incorrect", JOptionPane.INFORMATION_MESSAGE);
txuser.setText("");
pass.setText("");
txuser.requestFocus();
if (radNew.isSelected()) {
}
JOptionPane.showMessageDialog(null,
"Re-enter your password and ID to confirm", "Information Saved",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
}
}
# 1 楼答案
使用
而不是
因为
正如@MadProgrammer所说:
使用
Map
存储用户名/密码输入一些初始值
这是修改后的代码