简体   繁体   English

从文本字段获取字符串到另一个类的空值[Java]

[英]Getting String from textfield to another class null value[Java]

Hello I'm sorry if this is answered before, I'm really tired and bored :/ What I'm trying to do is getText() from textField in Class1 to String in Class2: 您好,很抱歉,如果以前没有回答,我真的很累和无聊:/我想做的是将getText()从Class1的textField转换为Class2的String:

In Class1: 在Class1中:

JTextField textField = new JTextField();
frame.getContentPane().add(textField);
. . .
    public String getme() {
    return textField.getText();
}

And to get String in Class2: 并在Class2中获取String:

String user = new Class1().getme();

This doesn't work.. it gets null value. 这不起作用..它获取空值。

If i put raw value, like: 如果我输入原始值,例如:

public String getme() {
    return "hm";
}

It does work. 确实有效。

Can you help, ty in advance! 您可以提前帮助吗?

You're creating a new Class1 with a new JTextField inside. 您正在使用新的JTextField创建一个新的Class1。 It's certainly the default behaviour of the getText() method on new objects. 当然,这是新对象上的getText()方法的默认行为。

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

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