在Java Swing中自动填充jTextfield [英] Auto filling a jTextfield in java swing

查看:348
本文介绍了在Java Swing中自动填充jTextfield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过netbeans使用swing创建一个小型系统(我必须说我是新来的swing).看下面的图片.

I am creating a small system using swing via netbeans(I must say i am new to swing). look at the below image .

在这个id ="ST100"的情况下,我需要在打开它时在以下界面中将其自动添加到jTextfield中

in this the id = "ST100" i need to add it to a jTextfield automatically in the below interface when i open it

因此,在使第二个界面中的箭头文本字段的访问修饰符为"public"之后,我将其编码为第一个界面源代码,如下所示.

so i coded it in the first interface source code as seen below after making the arrowed textfield's access modifier "public" which is in the second interface.

但是想要的文本没有自动填充,为什么?

but the wanted text is not auto filling why is it?

第一个界面中的相关代码

the relevent code in the first interface

addSubjects add = new addSubjects();
add.stid1.setText(stdid);

"stdid"是第一个接口文本字段的String输入. "addSubjects"是第二个接口的框架名称. "stid1"是第二个界面中带箭头的文本字段的变量名.

"stdid" is the String input of the 1st interface text field. "addSubjects" is the frame name of the 2nd interface. "stid1" is the variable name of the arrowed text field in the 2nd interface.

第二个界面中没有代码.我想要的是在第一个界面中单击提交"按钮后,第二个界面中带箭头的文本字段必须自动填充我给第一个带界面箭头的文本字段输入的相同字符串.

there are no codes in the second interface. what i want is to after clicking the submit button in the first interface the arrowed text field in the 2nd interface must autofill the same String which i give to the first interaface arrowed textfield.

推荐答案

因此,您希望将String值从一个类传递到另一个类.

So, you want to pass the String value from one class to another.

最简单和(最好)的方法是将第二类创建为第一类的内部类.

Well, the easiest and (best) way to achieve this is by creating the 2nd class as an inner class of the first class.

  • 所有您需要做的是将第二类代码粘贴到第一类代码中(不要将其粘贴到主方法中)

从第二类的主要方法中删除static

Remove the static word from your 2nd class's main method

在您的第一个类中声明一个字符串,如下所示,

Declare a String in your 1st class as follows,

String StudentId;

从用户那里收集ID后,将上述字符串初始化为

After you collect the Id from the user, intialize the above mentioned String as,

String StudentId = "student_id_to_show";

现在,在第二类代码(第一类的内部类)中,将jTextField的文本设置为

Now,in your 2nd class code(which is inner class of the 1st class), set the text of the jTextField as,

jTextField.setText(StudentId);

全部完成,干杯!

这篇关于在Java Swing中自动填充jTextfield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆