简体   繁体   English

如何从JTextField获取和传递值

[英]how to get and pass value from JTextField

I've created class with JTextField and I want to get this value and use in other class. 我已经用JTextField创建了类,我想获取该值并在其他类中使用。

public class Create{
 JTextField user;

public Create(){
  user =new JTextField(10);
  }
public  String getUser(){
  return user.getText();
  }



public class ReviewWindow{
 JLabel luser1;
 public ReviewWindow(){
  Create create = new Create();
  luser1= new JLabel(create.getUser()); //I want to get this value here.
    }
  }

So usually there is an action that is associated with getting a value from a JTextField. 因此,通常会有一个与从JTextField获取值相关的动作。 For example a button named "save" that is pressed once the user is finished typing in the JTextfield. 例如,在用户完成在JTextfield中的键入后,将按下一个名为“保存”的按钮。 So set up a button (an tutorial/explanation can be found here ) add an action listener to that button. 因此,设置一个按钮(可以在此处找到教程/说明),向该按钮添加一个动作侦听器。 Inside that actionlistener function, grab the text from JtextField (JtextField.getText()) and pass it to your other class via a getter/setter function or constructor. 在该actionlistener函数中,从JtextField(JtextField.getText())中获取文本,然后通过getter / setter函数或构造函数将其传递给其他类。

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

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