[英]Java Swing GUI Client and Server Chat App TextArea not updating
[英]callback to update gui, java - swing - textArea
我正在尝试使用播放器名称更新GUI textArea,但似乎无法在GUI类实例中调用任何方法/变量。 AddPlayerButtonAction构造函数将链接添加到其他类,但是只有gameEngine链接有效。
public AddPlayerButtonAction(JFrame gameFrame, GameEngineImpl gameEngine) {
this.gameFrame = gameFrame;
this.gameEngine = gameEngine;
}
public void actionPerformed(ActionEvent event) {
................does stuff....................
gameEngine. //Eclipse shows all gameEngine methods I can use
gameFrame. //only shows JFrame methods, no GUI class methods/variables
}
这是我的GUI类的代码,使所有变量在整个类中都可用并公开
public class GUI {
public JTextArea display = new JTextArea(5, 40);
final GameEngineImpl gameEngine = new GameEngineImpl();
public JFrame gameFrame = new JFrame("");
public GUI() {
addPlayerPanel(gameFrame);
addButtons(gameEngine, gameFrame);
//Passed gameFrame and gameEngine
}
还有我的GUI类中的PlayerPanel方法
public void addPlayerPanel(JFrame gameFrame) {
gameFrame.add(this.playerPanel);
display.setEditable(true); // set textArea to editable
JScrollPane scroll = new JScrollPane(display);
playerPanel.add(scroll);
}
通过仅将关键对象分配给局部变量(这些变量仅存在于方法或构造函数中,因此仅在声明的方法或构造函数中可见)来掩埋关键对象,这将阻止您在何时何地访问这些对象需要。 我不建议您将所有内容都暴露给外界,但是您确实需要为类提供私有实例字段,以供外部对象需要访问的关键对象使用。 然后,您应该以有限的方式并使用公共方法而非公共字段来授予访问权限。
例如,您在这里有以下代码:
void addPlayerPanel(JFrame gameFrame) {
JPanel playerPanel = new JPanel();
gameFrame.add(playerPanel, BorderLayout.CENTER);
playerPanel.setBorder(new TitledBorder(new EtchedBorder(), "Registered players"));
// display is a local variable, and only visible in this method
JTextArea display = new JTextArea(5, 40);
display.setEditable(true); // set textArea to editable
JScrollPane scroll = new JScrollPane(display);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
playerPanel.add(scroll);
display.append("test3\n");
}
在这里,您可以很好地创建一个JTextArea并将其添加到JFrame中,但是分配给JTextArea显示的变量在此方法中声明,仅在此方法中存在,因此仅在此方法中可见。 如果您的程序需要通过其他任何方式访问JTextArea,那么您将很难获得引用的句柄。 而是执行以下操作:
class MyGui {
// **** private instance field for a key object of your class ****
private JTextArea display;
void addPlayerPanel(JFrame gameFrame) {
JPanel playerPanel = new JPanel();
gameFrame.add(playerPanel, BorderLayout.CENTER);
playerPanel.setBorder(new TitledBorder(new EtchedBorder(), "Registered players"));
// *** note the change? ***
// JTextArea display = new JTextArea(5, 40);
display = new JTextArea(5, 40);
display.setEditable(true); // set textArea to editable
JScrollPane scroll = new JScrollPane(display);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
playerPanel.add(scroll);
display.append("test3\n");
}
// The two methods below allow limited access to your private display field
public String getDisplayText() {
return display.getText();
}
public void appendToDisplay(String text) {
display.append(text + "\n");
}
}
注意事项:
gameFrame. //only shows JFrame methods, no GUI class methods/variables
这是因为gameFrame是JFrame变量,而不是MyGUI或GUI变量。 Java(不是Eclipse)将仅允许您调用变量类型拥有的方法。 对象类型对此并不重要,而是变量类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.