简体   繁体   中英

callback to update gui, java - swing - textArea

I'm trying to update my GUI textArea with player names but can't seem to call any methods/variables in the GUI class instance. The AddPlayerButtonAction constructor adds links to the other classes but only gameEngine link works.

  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

}

This is the code for my GUI class, made all variable available class wide and public

 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
}

And the PlayerPanel method in my GUI class

public void addPlayerPanel(JFrame gameFrame) {
 gameFrame.add(this.playerPanel);
  display.setEditable(true); // set textArea to editable
  JScrollPane scroll = new JScrollPane(display);
  playerPanel.add(scroll);
 }

You're burying key objects by only assigning them to local variables, variables that only exist within methods or constructors, and thus are only visible within the method or constructor of declaration, and this is preventing you from gaining access to these objects when and where needed. I don't recommend that you expose everything to the outside world, but you do need to give your class private instance fields for the key objects that outside objects need access to. And then you should grant access but in a limited fashion and by using public methods, not public fields.

For instance, you have this code here:

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");                    
}

Here you create a JTextArea and add it to a JFrame, all well and good, but the variable that you assign the JTextArea to, display, is declared within this method, only exists within this method and is thus only visible within this method. If your program needs access to the JTextArea in any way elsewhere, you're going to have a difficult time getting a handle on the reference. Instead do something like:

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");
    }

}

Note regarding:

gameFrame. //only shows JFrame methods, no GUI class methods/variables

And this is because gameFrame is a JFrame variable not a MyGUI or GUI variable. Java (not Eclipse) will only allow you to call the method that a variable type owns. Object type isn't what matters for this, rather variable type does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM