简体   繁体   English

如何通过按下JButton将文本从一个JTextField添加到另一个

[英]How to add text from one JTextField to another by pressing a JButton

I am creating a applet in java where the user enters some data into four different four different text fields, clicks a button, and the data is taken from those fields and stored in a text field next to it. 我在Java中创建一个applet,用户在其中将数据输入到四个不同的四个不同文本字段中,单击一个按钮,然后从这些字段中获取数据并存储在其旁边的文本字段中。

I have a button made, and it implements ActionListener, just not sure how to get the text from the four fields into the one field together. 我做了一个按钮,它实现了ActionListener,只是不确定如何将四个字段中的文本放到一个字段中。

Try this. 尝试这个。

tf1.setText(tf2.getText());

OR When you want to merge values from all textfields into one. 或当您要将所有文本字段中的值合并为一个。

tf1.setText(tf1.getText() + tf2.getText() + tf3.getText() + tf4.getText());

just not sure how to get the text from the four fields into the one field together. 只是不确定如何将四个字段中的文本放到一个字段中。

JTextField has a getText() method. JTextField有一个getText()方法。 So you would invoke that method on the 4 different text field to get the String value from each text field. 因此,您将在4个不同的文本字段上调用该方法,以从每个文本字段获取String值。

JTextField also has a setText() method. JTextField也具有setText()方法。 So you would combine the 4 strings into one and set the text of the 5th text field. 因此,您可以将4个字符串组合为一个,并设置第5个文本字段的文本。

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

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