[英]How to access to the value inside a Swing JTextField/JPasswordField?
I am pretty new in Java Swing development and I have the following problem. 我是Java Swing开发中的新手,我有以下问题。
I have a custom LoginFrame that extends a classic JFrame Swing class to create a login windows in which the user insert its username and password. 我有一个自定义的LoginFrame ,它扩展了一个经典的JFrame Swing类来创建一个登录窗口,用户可以在其中插入用户名和密码。
Inside this class I have something like this: 在这堂课里面,我有这样的事情:
externalPanel.setLayout(new net.miginfocom.swing.MigLayout("fill"));
externalPanel.add(new JLabel("Username"), "w 50%, wrap");
JTextField userNameTextField = new JTextField(20);
externalPanel.add(userNameTextField, "w 90%, wrap");
externalPanel.add(new JLabel("Password"), "w 50%, wrap");
// JTextField pswdTextField = new JTextField(20);
JPasswordField pswdTextField = new JPasswordField(20);
externalPanel.add(pswdTextField, "w 90%, wrap");
JButton loginButton = new JButton("Login");
// loginButton.setActionCommand("loginAction");
loginButton.addActionListener(this);
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("Button LogIn cliccked");
firePropertyChange("loginResult", false, loginResult);
}
As you can see I have the actionPerformed that is executed when the user click the JButton loginButton object. 如您所见,我有在用户单击JButton loginButton对象时执行的actionPerformed。
Ok, my problem is: From inside the actionPerformed()^^ method how can I access to the values inserted in my **JTextField userNameTextField and JPasswordField pswdTextField ? 好吧,我的问题是:从actionPerformed()^^方法内部如何访问我的** JTextField userNameTextField和JPasswordField pswdTextField中 插入的值 ?
What have I to do? 我该怎么办?
Tnx TNX
Andrea 安德里亚
Declare JTextField userNameTextField, JPasswordField pswdTextField, JButton loginButton
as globel. 声明
JTextField userNameTextField, JPasswordField pswdTextField, JButton loginButton
as globel。 Then you can get values like this.. 然后你可以得到这样的价值..
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == loginButton){
String name = userNameTextField.getText();
char[] pass = pswdTextField.getPassword();
// your remaining operation...
}
}
For JTextField: jTextField.getText() ---> returns String 对于JTextField:jTextField.getText()--->返回String
For JPasswordField: jPasswordField.getPassword() ----> returns char[] 对于JPasswordField:jPasswordField.getPassword()---->返回char []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.