在 Java 中使用 JTextField 对象

发布于 2025-01-01 14:24:37 字数 372 浏览 3 评论 0原文

我确实承认这对你们许多人来说是一个愚蠢的问题!首先我想说,就图形用户界面而言,我的java知识还很低!我的文本字段有问题。我想获取一个 JTextField 对象的值并将其显示在另一个 JTextField 对象中!这是我尝试过的,但它不起作用!

       textField2.setText(textField1.getText());

问题是我有两个框架对象,每个对象都有一个 textField 对象,我想将一个值从 jFrame1jTextField1 复制到 jTextField2jFrame2

I do accept that this is a dumb question to many of you! First of all i want to say that my java knowledge is low as far as graphical user interface is concerned! I have a problem with textFields. I want to get the value of one JTextField object and display it in another JTextField object!This is what i tried but it is not working!

       textField2.setText(textField1.getText());

The problem is that i have two frames objects and each has a textField object and i want to copy one value from jTextField1 of jFrame1 to jTextField2 of jFrame2 .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦初醒的夏天 2025-01-08 14:24:38

问题是有效的,您提供的信息量却无效。在您提供足够的详细信息以便我们了解问题所在之前,我们无法为您提供帮助。

不过,戴上我的读心帽,我猜你的问题是你在程序启动时调用上面的这个方法,并期望如果你更新一个 JTextField,另一个将自动更新,但事实并非如此。当您进行此调用时:

textField2.setText(textField1.getText());

您所做的就是将第一个字段中保存的字符串放入第二个字段中。在程序启动时,这可能为空,但即使它包含文本,字符串也是不可变的,并且永远不会改变,即使第一个字段的文本发生变化。

如果您希望一个字段始终保存与另一个字段相同的文本,请让它们共享相同的模型

textField2.setDocument(textField1.getDocument()); // * edited per mKorbel's rec

另一方面,如果您的目标是从一个字段获取文本JTextField 并将其放入另一个中,但仅当用户选择执行此操作时,然后使用添加到 JButton 或第一个 JTextfield 本身的 ActionListener(通过在插入符号位于字段中时按 Enter 键激活),以及在那个监听器中,放置你的行 代码:

textField2.setText(textField1.getText());

The question is valid, the amount of information that you provide is not. There's no way to help you til you tell enough details so that we can understand what's wrong.

Putting my mind-reading hat on though, I am going to guess that your problem is that you make this method call above on program start up, and expect that if you update one JTextField, the other will be updated automatically, but that's not so. When you make this call:

textField2.setText(textField1.getText());

All you're doing is placing the String held in the first field into the second field. On program start up, this may be null, but even if it contained text, the String is immutable and will never change, even if the 1st field's text changes.

If you want one field to always hold the same text as the other, have them share the same model:

textField2.setDocument(textField1.getDocument()); // * edited per mKorbel's rec

If your goal on the other hand is to get the text from one JTextField and put it into another but only when the user chooses to do this, then use an ActionListener that is added to either a JButton or to the first JTextfield itself (which is activated by pressing enter while the caret is in the field), and in that listener, place your line of code:

textField2.setText(textField1.getText());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文