【发布时间】:2016-12-31 07:22:47
【问题描述】:
我使用 Java Swing 创建了一个登录框架 - 登录框架 (1),其中也发生了用户身份验证。
-
看起来像这样:
因此,如果用户输入错误的电子邮件 ID/密码,它应该显示一个消息对话框。这就是我在这里使用的原因:
JOptionPane.showMessageDialog( this,"plese enter correct id/password")
但它显示错误..
Login.addActionListener((e) -> {
ArrayList<Registration> list0;
list0=UserDataReadWriteFromFile.readDataFromFile();
int idpos=Search.searchId(tuid.getText().trim());
if(idpos >=0){
String ueid=tuid.getText().trim();
String uupass=tpass.getText().trim();
if(ueid.equals(list0.get(idpos).getId())&& uupass.equals(list0.get(idpos).getPassword())){
new SearchDisp(idpos);
}
else
JOptionPane.showMessageDialog( this,"plese enter correct id/password");
}
});
我应该在JOptionPane.showMessageDialog() 中使用什么组件?
这是我的登录框架的代码。在这个带有登录按钮的代码中,我添加了动作监听器,它是用我问过的上面的代码编写的。
public class LoginFrame{
public LoginFrame() {
JLabel uid, upass;
JTextField tuid;
JPasswordField tpass;
JButton Login;
JFrame frame = new JFrame("Login");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLayout(null);
JLabel background = new JLabel(new ImageIcon(
"C:\\Users\\Tousif\\Desktop\\Login.jpg"));
frame.setContentPane(background);
uid = new JLabel("Email Id");
uid.setBounds(60, 50, 120, 25);
frame.add(uid);
tuid = new JTextField(20);
tuid.setBounds(120, 50, 150, 24);
frame.add(tuid);
upass = new JLabel("Password");
upass.setBounds(53, 80, 120, 25);
frame.add(upass);
tpass = new JPasswordField(20);
tpass.setBounds(120, 80, 150, 24);
frame.add(tpass);
Login = new JButton("Login");
Login.setBounds(150, 110, 80, 25);
frame.add(Login);
frame.setSize(370, 216);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] arg) {
new LoginFrame();
}
}
【问题讨论】:
-
错误是什么?
-
JOptionPane 类型中的方法 showMessageDialog(Component, Object) 不适用于参数(LoginFrame, String)
-
String uupass=tpass.getText().trim();密码字段应该是JPasswordField,密码永远不能以String的形式获得,而是以字符数组的形式获得(char[])因为字符串对象可以保留在内存中,从而提供攻击应用程序的向量。安全。 “我应该在JOptionPane.showMessageDialog()中使用什么组件?” JavaDocs 对此提供了简短的通用答案。为什么不看看他们? -
代替
setBound(),试试这个layout。另请参阅Initial Threads。 -
对于example。不要让我更换供应商。 :-)
标签: java swing compiler-errors joptionpane