java - 无法更改 JTextField 文本

标签 java jtextfield

我通常用 C# 编写代码,甚至用 C# 完成应用程序,但我需要将其移植到 Java。

今天刚开始,以前从未真正使用过Java。

这个问题很奇怪,因为我没有收到任何错误,它只是不更新​​,代码如下:

public void setInfoStrip(String infoStrip) {
    InfoStrip.setText(infoStrip);
}

上面是一个应该更新 JTextField 的 Setter(如果我使用按钮的 ActionListener 更新它,它就会更新),但是当我使用以下代码在类中甚至在应用程序的 main(String[] args) 入口点中调用它时,它不会更新文本:

mainGUI GUI = new mainGUI();
GUI.setInfoStrip("test");

或者这段代码:

new mainGUI().setInfoStrip("test");

我的猜测是它什么也没做,因为我从静态类调用它

public static void main(String[] args)

但即使我创建一个非静态的新类并从 public staitc void main(String[] args) 引用它,然后放置

mainGUI GUI = new mainGUI();
GUI.setInfoStrip("test");

new mainGUI().setInfoStrip("test");

它是新创建的类,我称之为

new ImGoingToCry().Alot();

它仍然什么也没做。

我很困惑,我什至在谷歌上读到了一些与此相关的问题,但它们都解决了:

mainGUI GUI = new mainGUI();
GUI.setInfoStrip("test");

这是你们中的一些人请求的 MVCE:

public class mainGUI {
// GUI Elements
private JPanel WorkSpace;
private JTabbedPane tabbedPane1;
private JList DetectedProfiles;
private JButton StartGame;
private JTextField CurProf;
private JButton BackupProfiles;
private JButton SearchSaves;
private JButton RetrieveProfiles;
private JTextField InfoStrip;
private JLabel ProfileSize;

/**
 * Getter and Setter functions
 */
public void setInfoStrip(String infoStrip) {
    InfoStrip.setText(infoStrip);
}


// Initialize the main application GUI and set it's properties
public static void main(String[] args) {
    JFrame mainGUIFrame = new JFrame("The Witcher 3 Save Manager | " + " ver. "  + GlobalVariables.appversion);
    mainGUIFrame.setContentPane(new mainGUI().WorkSpace);
    mainGUIFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainGUIFrame.setLocationRelativeTo(null);
    mainGUIFrame.setPreferredSize(new Dimension(420, 370));
    mainGUIFrame.setResizable(false);
    mainGUIFrame.pack();
    mainGUIFrame.setVisible(true);
    new mainGUI().run();
}


public void run() {
    /**
     * Initialize the core application functions
     */
    // Load the application settings
    GlobalVariables.Settings();
    // Initialize the app components
    GlobalVariables.Initialize();

    // Pass the value to setter
    new mainGUI().setInfoStrip("test"); // This should change the text but it does nothing there's not even an error
}
}

最佳答案

也许这会有所帮助。来自 javadocs JTextComponent ,其中 JTextField 继承 setText() 方法:

setText

Sets the text of this TextComponent to the specified text. If the text is null or empty, has the effect of simply deleting the old text. When text has been inserted, the resulting caret location is determined by the implementation of the caret class.

Note that text is not a bound property, so no PropertyChangeEvent is fired when it changes. To listen for changes to the text, use DocumentListener.

如果您想知道 BoundProperty 是什么?是:

A bound property notifies listeners when its value changes. This has two implications:

  1. The bean class includes addPropertyChangeListener() and removePropertyChangeListener() methods for managing the bean's listeners.
  2. When a bound property is changed, the bean sends a PropertyChangeEvent to its registered listeners. PropertyChangeEvent and PropertyChangeListener live in the java.beans package.

The java.beans package also includes a class, PropertyChangeSupport, that takes care of most of the work of bound properties. This handy class keeps track of property listeners and includes a convenience method that fires property change events to all registered listeners.

P.S.:因为OP没有给出任何mcve ,这听起来很相关。但是,我强烈认为,一旦我们看到 OP 的代码,情况就会有所不同。

关于java - 无法更改 JTextField 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35197678/

相关文章:

java - 设置文本背景颜色?

java - FocusEvent如何输入字段和使用按钮

java - JButton 和 JTextField 的连接?

java - 如果字符串以负号开头,setText 将无法正常工作

java - 如何实例化AsyncTask

java - Docker + Tomcat + .properties -- 环境变量

java - AXIS2 的 WEBService 基本身份验证

java - 使用 ThreadLocal 与以 Thread 作为键的 HashMap

java - 无法 Autowiring 自定义用户详细信息

java - Java 中同一 GridBagLayout 中 JTextField 和 JComboBox 的问题