[英]Write the value of a JTextField to a variable of type String from another class (JAVA)
I am very difficult to record the value typed into a JTextField that belongs to class 1 in variable of type String that belongs to class 2, for example, if I type the word "Test" in the JTextField class 1 when i press the button in class 1, I would like the value entered in the JTextField class 1 is recorded on a variable type String in class 2, so I use this variable as I want, can you give me a hand with that? 我很难在属于类2的String类型的变量中记录键入到属于类1的JTextField中的值,例如,当我按下按钮时,在JTextField类1中键入单词“ Test”类1,我希望在JTextField类1中输入的值记录在类2中的变量类型String上,所以我可以根据需要使用此变量,您能帮我吗? I've tried to create an object of class 1 and use the getText and to String, but no success yet.
我试图创建一个类为1的对象,并使用getText和String,但是还没有成功。 When i see the System in the class2 the result is NULL !
当我在class2中看到System时,结果为NULL! :(
:(
Thank you. 谢谢。
The code : 编码 :
public class Class1 extends javax.swing.JFrame {
public Class1() {
initComponents();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
fieldOfClass1 = new javax.swing.JTextField();
bottonOfClass1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
getContentPane().add(fieldOfClass1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 39, 336, 38));
bottonOfClass1.setText("Botton");
bottonOfClass1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bottonActionPerformed(evt);
}
});
getContentPane().add(bottonOfClass1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 114, 161, 45));
pack();
}// </editor-fold>
private void bottonActionPerformed(java.awt.event.ActionEvent evt) {
Class2 classTwo = new Class2();
classTwo.receiveFieldValueOfClass1= returnField();
System.out.println(classTwo.receiveFieldValueOfClass1);
}
String returnField(){
return fieldOfClass1.getText().toString();
}
public static void main(String args[]) {
new Class1().setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JButton bottonOfClass1;
private javax.swing.JTextField fieldOfClass1;
// End of variables declaration
}
Class 2 : 第2类:
public class Class2 {
public String receiveFieldValueOfClass1;
Class2(){
System.out.println("This is a valor of Jtext Field Class 1 ! = "+receiveFieldValueOfClass1);
}
public static void main (String[]args){
}
}
Thanks 谢谢
IIRC, you'd like to set a variable (let's name it s) in Class2 to the value of JTextField in Class1? IIRC,您想将Class2中的变量(以它的名字命名)设置为Class1中JTextField的值吗?
Just add a public(!) method to Class2 that takes a String as argument and sets the value of s to the given String. 只需向Class2添加public(!)方法,该方法将String作为参数并将s的值设置为给定的String。
In Class1 just create an ActionListener for the button which calls the public method of Class2 with the value of JTextField as argument. 在Class1中,只需为按钮创建一个ActionListener即可,该按钮使用JTextField的值作为参数调用Class2的公共方法。
I could help you more if you'd include a code sample in your question. 如果您要在问题中加入代码示例 ,我们可以为您提供更多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.