简体   繁体   English

将文本从Jtextfield(Jframe)发送到另一个类

[英]Send Text From Jtextfield (Jframe) To another class

i put text on a JTextfield and i try to use that text in another class ... i have no error but no result 我将文本放在JTextfield上,并尝试在另一个类中使用该文本...我没有错误,但没有结果

    btnEntrez.addActionListener(new AuthentificationListner());

public class AuthentificationListner implements ActionListener{ 公共类AuthentificationListner实现ActionListener {

@Override
public void actionPerformed(ActionEvent e) {

    Authentification aut = new Authentification();
    String login = aut.txtnom.getText();
    System.out.println("Login :"+login);

}

result : Login : (But no textfield text :/ ) 结果:登录:(但没有文本字段文本:/)

If Authentification is a JFrame , then aut.txtnom.getText() isn't going to return anything (other than what it was initialized with) as the user won't have time to enter anything before you try and grab the value, this is what JDialog is for. 如果AuthentificationJFrame ,则aut.txtnom.getText()不会返回任何内容(除了初始化时使用的内容除外),因为用户在尝试获取值之前没有时间输入任何内容,因此是JDialog目的。 See How to make dialogs for more details. 有关更多详细信息,请参见如何进行对话框

Swing, like most UI frameworks is event driven, something happens, you respond to it, rather the procedural or linear (which your code seems to be trying to do) 像大多数UI框架一样,摆动是事件驱动的,会发生某些事情,您会对此做出响应,而不是过程性的或线性的(您的代码似乎正在尝试这样做)

Another approach is to use an observer pattern on Authentification , which will notify interested parties that something (they might be interested in) has changed. 另一种方法是在Authentification上使用观察者模式,该模式将通知感兴趣的各方(他们可能感兴趣的东西)已更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM