JAVA Swing - Setting JLabel equal to a String in another class

前端 未结 3 912
死守一世寂寞
死守一世寂寞 2020-12-21 21:02

I have a method called getTheUserInput in a class which returns a String which updates based on user actions, which is located inside a class called Board.java.

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-21 21:25

    You'd .... need to update the JLabel.

    myJLabel.setText(newString);
    

    Strings in Java are immutable so they can never change / be changed.

    For example when you say, "I have action which adds data to a String which I know works." ... you're incorrect. You've created a new String. You need to supply the JLabel with the new String (the user's input) if you want to change the text.

    Edit: To answer the last part of your question; you'd need to keep track of the JLabel and update it as I show above every time the user provides input (in the event handler for whatever that is). Using the Observer Pattern might be an option as Java provides it via Observer and Observable

提交回复
热议问题