I want to do the following: As soon as a specific Variable (roomName) changes its Value, the Title of a JFrame should be changed to the new Value of roomName. My only problem is, that the JFrame is already built before the roomName changes.
我想执行以下操作:只要特定变量(roomName)更改其值,JFrame的标题就应更改为roomName的新值。我唯一的问题是,在roomName更改之前已经构建了JFrame。
This is a little snippet of my Connection.java Class:
这是我的Connection.java类的一小部分:
public Connection() {
...
fieldName.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if(e.getKeyChar() == KeyEvent.VK_ENTER) {
setName();
}
}
});
}
public void setName(){
ChatFrame.frame.setVisible(true);
ChatFrame.roomName = fieldName.getText();
this.dispose();
}
The other Class ChatFrame.java should do the described Action above. Do I need a Listener or a Thread for this? What's the best way to do it?
另一个类ChatFrame.java应该执行上面描述的Action。我需要一个监听器或线程吗?最好的方法是什么?
2 个解决方案
#1
3
One simple solution that springs to mind is to wrap the variable up into an object, then the setter method can look like this:
我想到的一个简单的解决方案是将变量包装到一个对象中,然后setter方法可以如下所示:
public void setNewValue(String newVal)
{
if(!newVal.equals(currentVal)) {
currentVal = newVal;
// Value has changed. Call the relevant code.
}
}
#2
2
You should make the ChatFrame
variable private to start with, exposing a method to change it:
您应该将ChatFrame变量设为私有,以便公开更改它的方法:
public void setRoomName(String newName) {
...
}
Then you can add a listener for that, and trigger it within the method.
然后,您可以为其添加一个侦听器,并在该方法中触发它。
Note that this should logically be an instance variable, not a static variable as it appears to be at the moment.
请注意,这在逻辑上应该是一个实例变量,而不是目前看来的静态变量。
(If that's not what you were looking for, please edit your question. It's very unclear at the moment, partly as we're not sure where the setName
method is, or whether you've really got static variables for roomName
and frame
.)
(如果那不是你想要的,请编辑你的问题。目前还不清楚,部分原因是我们不确定setName方法在哪里,或者你是否真的得到了roomName和frame的静态变量。)
#1
3
One simple solution that springs to mind is to wrap the variable up into an object, then the setter method can look like this:
我想到的一个简单的解决方案是将变量包装到一个对象中,然后setter方法可以如下所示:
public void setNewValue(String newVal)
{
if(!newVal.equals(currentVal)) {
currentVal = newVal;
// Value has changed. Call the relevant code.
}
}
#2
2
You should make the ChatFrame
variable private to start with, exposing a method to change it:
您应该将ChatFrame变量设为私有,以便公开更改它的方法:
public void setRoomName(String newName) {
...
}
Then you can add a listener for that, and trigger it within the method.
然后,您可以为其添加一个侦听器,并在该方法中触发它。
Note that this should logically be an instance variable, not a static variable as it appears to be at the moment.
请注意,这在逻辑上应该是一个实例变量,而不是目前看来的静态变量。
(If that's not what you were looking for, please edit your question. It's very unclear at the moment, partly as we're not sure where the setName
method is, or whether you've really got static variables for roomName
and frame
.)
(如果那不是你想要的,请编辑你的问题。目前还不清楚,部分原因是我们不确定setName方法在哪里,或者你是否真的得到了roomName和frame的静态变量。)