java 将.getText() 字符串传递给另一个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10067399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing.getText() String to another class
提问by DanMc
I'm currently working on a first year university project and I have a problem, although I doubt it's a very complicated one, but I've been searching and I just can't find a suitable answer to it.
我目前正在从事大学一年级项目,我遇到了一个问题,虽然我怀疑这是一个非常复杂的问题,但我一直在寻找,但找不到合适的答案。
The problem concerns two classes. A gui class (class1) and another class (class2), when I run the code the second class gives me a nullpointerexception when trying to retrieve the getText() String in class2 from class1.
问题涉及两个类。一个gui类(class1)和另一个类(class2),当我运行代码时,第二个类在尝试从class1检索class2中的getText()字符串时给了我一个空指针异常。
I have a JTextField in class1 and am trying to pass through the .getText() value to class2 and store it in a String type variable.
我在 class1 中有一个 JTextField 并试图将 .getText() 值传递给 class2 并将其存储在 String 类型变量中。
The current code I'm trying to achieve this with is the following (simplified):
我正在尝试使用的当前代码如下(简化):
public class Class1 {
private JTextField textField = new JTextField("Something");
public Class1() {}
...
public String getTextFieldString() {
return textField.getText();
}
}
public class Class2 {
public Class2(Class1 class1) {
c1=class1
}
private c1 Class1 = new Class1();
private String s = new String();
...
s = c1.getTextFieldString();
}
I'm pretty new to coding, I've read that maybe I need to pass through an argument somewhere and I assume that's because textField is not static in itself, it changes when somebody enters a new value. (sorry for stating the obvious there.)
我对编码很陌生,我读过可能我需要在某处传递一个参数,我认为这是因为 textField 本身不是静态的,当有人输入新值时它会发生变化。(很抱歉在那里说明了明显的问题。)
Anyway, help is appreciated. Thanks a lot!
无论如何,感谢帮助。非常感谢!
采纳答案by trashgod
I may help to review the prescribedinitialization orderin Java. In particular, the textField
instance variable initializer should be complete by the time a valid Class1
instance exists. One way to violate this order is to construct Class1
and Class2
on different threads. To preclude this, Swing GUI objects should be constructed and manipulated onlyon the event dispatch thread, as shown in the ssccebelow.
我可能会帮助查看Java 中规定的初始化顺序。特别是,textField
实例变量初始值设定项应该在有效Class1
实例存在时完成。违反此顺序的一种方法是在不同的线程上构造Class1
和Class2
。为了避免这种情况,Swing GUI 对象应该只在事件分派线程上构造和操作,如下面的sscce所示。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/** @see http://stackoverflow.com/q/10067399/230513 */
public class NewJavaGUI extends JPanel {
public NewJavaGUI() {
Class1 class1 = new Class1();
Class2 class2 = new Class2(class1);
this.add(class1.textField);
this.add(new JLabel(class2.s));
}
private static class Class1 extends Object {
private final JTextField textField = new JTextField("Something");
public Class1() {} // implicit
public String getTextFieldString() {
return textField.getText();
}
}
private static class Class2 extends Object {
private final Class1 c1;
private final String s;
public Class2(Class1 class1) {
c1 = class1;
s = c1.getTextFieldString();
}
}
private void display() {
JFrame f = new JFrame("NewJavaGUI");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new NewJavaGUI().display();
}
});
}
}
回答by óscar López
Well, you could pass a reference to Class1
in the constructor of Class2
and save it in an attribute, and every time Class2
needs the text, simply call the getText()
method on the attribute.
好吧,你可以Class1
在的构造函数中传递一个引用Class2
并将其保存在一个属性中,每次Class2
需要文本时,只需调用getText()
属性上的方法即可。
public class Class2 {
private Class1 class1;
public Class2(Class1 reference) {
class1 = reference;
}
public void someMethod() {
String s = class1.getTextFieldString();
}
}
Now, when you instantiate a Class2
object:
现在,当你实例化一个Class2
对象时:
Class1 c1 = new Class1();
Class2 c2 = new Class2(c1);
回答by Dave
It sounds like you're interested in knowing when someone modifies the text in the text field. My first piece of advice is to look at the java tutorials whenever you have a specific question about a Swing component. Specifically, look at the addActionListener method in the JTextField tutorial, including the Text Demoexample.
听起来您很想知道何时有人修改了文本字段中的文本。我的第一条建议是,每当您有关于 Swing 组件的特定问题时,请查看 Java 教程。具体看JTextField教程中的addActionListener方法,包括Text Demo示例。