创建后如何在 JTextField 中设置新文本?
我有一个 jTextField ,当我创建框架时,我将其值设置为一定的总和。
这是启动代码:
totalTextField.setText(
itemsPriceTextField.getText() +
Float.toString(orderDetails.delivery)
);
此文本字段应显示用户选择的项目的总和。
选择是在不同的框架上完成的,并且两个框架都是可见/不可见的 一次。
用户可以来回添加/删除项目。
现在,每次我再次设置此框架可见时,我都需要重新加载设置到该字段的值
(也许没有进行任何更改,但如果是这样,我需要设置新的正确总和)。
我对此感到非常绝望。
谁能给我一个线索吗?
提前致谢! :)
I have a jTextField , and I set it's value to a certain sum when I create the frame.
Here is the initiation code:
totalTextField.setText(
itemsPriceTextField.getText() +
Float.toString(orderDetails.delivery)
);
This textfield should show a sum of items selected by the user.
The selection is done on a different frame, and both frames are visible / invisible
at a time.
The user can go back and forth and add / remove items.
Now, every time i set this frame visible again, I need to reload the value set to that field
(maybe no changes were made, but if so, I need to set the new correct sum) .
I'm quite desperate with it.
Can anyone please give me a clue?
Thanks in advance! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在将框架再次设置为可见之前,应使用新值/状态更新字段。
类似于:
框架将提出新的值/状态。
Before setting the frame visible again, one should update the fields with the new values / states.
something like:
The frame will come up with the new values / states.
将 WindowListener 添加到框架。然后您可以处理 windowActivated 事件并重置文本字段的文本。
请参阅如何编写窗口侦听器。
Add a WindowListener to the frame. Then you can handle the windowActivated event and reset the text of the text field.
See How to Write Window Listeners.
使用 DocumentListener 触发 JTextField public void setText(String t)
下面是一个 DocumentListener 示例:
灵感来自: <一href="https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TextFieldDemoProject/src/components/TextFieldDemo .java" rel="nofollow noreferrer">https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TextFieldDemoProject/src/components/TextFieldDemo .java
相关课程: https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html
Use a DocumentListener triggering the JTextField public void setText(String t)
Here an example with DocumentListener:
inspired from: https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TextFieldDemoProject/src/components/TextFieldDemo.java
related lesson: https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html