简体   繁体   English

将JTextField传递给另一个类

[英]Passing JTextField to another class

I am very new to Java and I am trying to pass a JTextField to another class. 我对Java非常陌生,并且正在尝试将JTextField传递给另一个类。 Here is my code: 这是我的代码:

Method to send the JTextField from SellWindow class: SellWindow类发送JTextField的方法:

public JTextField sendTextField(){
    return addItemField;
}

Method to get the JTextField in EnterAction class: EnterAction类中获取JTextField的方法:

public JTextField getItemField(){
    SellWindow field=new SellWindow();
    addItem=field.sendTextField();
    return addItem;
}

Executing the data in EnterAction class: EnterAction类中执行数据:

public void enterData(){
    System.out.println(this.getItemField().getText()); //output blank 
    System.out.println(this.addItem); //output "null"        
} 

I don't know where is the actual problem. 我不知道实际的问题在哪里。 After executing the code, it gives the following output as stated in the code's comments. 执行代码后,它会提供以下输出,如代码注释中所述。 It will be very helpful if someone could fix this problem. 如果有人可以解决此问题,将非常有帮助。 I am very new to Java and it's my first programe. 我对Java非常陌生,这是我的第一个程序。

It's not entirely clear what you're trying to do, but from what I can tell, you have a SellWindow frame and an EnterAction frame, and you want to pass user input between the two. 目前尚不清楚您要做什么,但据我所知,您有一个SellWindow框架和一个EnterAction框架,并且希望在两者之间传递用户输入。 One issue I see is that in the getItemField method, you are creating a new SellWindow instance but not displaying it or allowing the user to interact with it at all. 我看到的一个问题是,在getItemField方法中,您正在创建一个新的SellWindow实例,但不显示它或完全不允许用户与其交互。 Instead, I would make SellWindow as a JDialog and once the user closes the dialog, get the input. 取而代之的是,我将SellWindow做成JDialog,一旦用户关闭对话框,就得到输入。 Try this: 尝试这个:

public JTextField getItemField(){
    SellWindow field=new SellWindow(null, true);
    field.setVisible(true);
    addItem=field.sendTextField();
    return addItem;
}

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

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