创建后如何在 JTextField 中设置新文本?

发布于 2024-11-15 20:06:03 字数 434 浏览 4 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(3

黑色毁心梦 2024-11-22 20:06:03

在将框架再次设置为可见之前,应使用新值/状态更新字段。
类似于:

jTextField.setText("put your text here");  
jRadioButton.setSelected(!isSelected());  
.  
/* update all you need */
.  
jFrame.setVisible(true);

框架将提出新的值/状态。

Before setting the frame visible again, one should update the fields with the new values / states.
something like:

jTextField.setText("put your text here");  
jRadioButton.setSelected(!isSelected());  
.  
/* update all you need */
.  
jFrame.setVisible(true);

The frame will come up with the new values / states.

那伤。 2024-11-22 20:06:03

将 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.

一笑百媚生 2024-11-22 20:06:03

使用 DocumentListener 触发 JTextField public void setText(String t)

下面是一个 DocumentListener 示例:

public class SetTextInJTextField extends JFrame implements DocumentListener {

JTextField entry;
JTextField entryToSet = new JTextField();

public SetTextInJTextField() {
    createWindow();
    entry.getDocument().addDocumentListener(this);
}

private void createWindow() {
    JFrame frame = new JFrame("Swing Tester");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    createUI(frame);
    frame.setSize(560, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

private void createUI(final JFrame frame) {
    JPanel panel = new JPanel();
    entry = new JTextField();
    entryToSet = new JTextField();

    LayoutManager layout = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
    panel.setLayout(layout);

    panel.add(this.entry);
    panel.add(entryToSet);
    frame.getContentPane().add(panel, BorderLayout.CENTER);
}

public void setTextInTargetTxtField() {
    String s = entry.getText();
    entryToSet.setText(s);
}

// DocumentListener methods
public void insertUpdate(DocumentEvent ev) {
    setTextInTargetTxtField();
}

public void removeUpdate(DocumentEvent ev) {
    setTextInTargetTxtField();
}

public void changedUpdate(DocumentEvent ev) {
}

public static void main(String args[]) {
    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new SetTextInJTextField().setVisible(true);
        }
    });
  }
}

灵感来自: <一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:

public class SetTextInJTextField extends JFrame implements DocumentListener {

JTextField entry;
JTextField entryToSet = new JTextField();

public SetTextInJTextField() {
    createWindow();
    entry.getDocument().addDocumentListener(this);
}

private void createWindow() {
    JFrame frame = new JFrame("Swing Tester");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    createUI(frame);
    frame.setSize(560, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

private void createUI(final JFrame frame) {
    JPanel panel = new JPanel();
    entry = new JTextField();
    entryToSet = new JTextField();

    LayoutManager layout = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
    panel.setLayout(layout);

    panel.add(this.entry);
    panel.add(entryToSet);
    frame.getContentPane().add(panel, BorderLayout.CENTER);
}

public void setTextInTargetTxtField() {
    String s = entry.getText();
    entryToSet.setText(s);
}

// DocumentListener methods
public void insertUpdate(DocumentEvent ev) {
    setTextInTargetTxtField();
}

public void removeUpdate(DocumentEvent ev) {
    setTextInTargetTxtField();
}

public void changedUpdate(DocumentEvent ev) {
}

public static void main(String args[]) {
    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new SetTextInJTextField().setVisible(true);
        }
    });
  }
}

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

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