我想在View类中获取用户名和密码 但是从控制器类
如何通过从Controller类调用它来获取数据来使用loginbtnMouseClicked方法来获取数据
private void loginbtnMouseClicked(java.awt.event.MouseEvent evt) {
//what should i have here?
}
查看类代码
usernamefield = new javax.swing.JTextField();
passfield = new javax.swing.JPasswordField();
loginbtn = new javax.swing.JLabel(); //Login Button is JLabel
private void loginbtnMouseClicked(java.awt.event.MouseEvent evt) {
//what should i have here?
}
/*
*returns the username from the username field
*/
public String getUserName(){
return usernamefield.getText();
}
/*
*returns the password from the password field
*/
public char[] getPass(){
return passfield.getPassword();
}
控制器类代码
This is what am asking..........
模型类代码
如果用户名和密码都是admin
,则只有一个方法返回truepublic Boolean isAccountHolder(String username, char[] password){
return username.equalsIgnoreCase("admin") && password.equals("admin");
}
答案 0 :(得分:1)
首先,不确定为什么“loginbtn”会是JLabel
...
在View代码中,理想情况下您需要一些定义click方法的ActionListener。你没有宣布自己的......
(示例代码)
class MyView extends JPanel {
JTextField usernamefield = new javax.swing.JTextField();
JPasswordField passfield = new javax.swing.JPasswordField();
Model model = new Model();
public MyView() {
// Here you update the View
add(usernamefield);
add(passfield);
JButton button = new JButton("Click");
add(button);
// Here the Panel acts as the controller, and updates the model
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.setName(getUserName());
model.setPass(getPass());
}
});
}
public String getUserName(){
return usernamefield.getText();
}
public char[] getPass(){
return passfield.getPassword();
}
}
另外,请注意:password.equals("admin")
不适用于char[]
,因为数组不等于字符串。您必须使用new String(password).equals("admin")
,但如果您使用的是纯文本密码,则这是一个完全独立的问题。