Changing text on a JLabel from another class

给你一囗甜甜゛ 提交于 2019-12-01 15:01:14
Elliott Frisch

First, add a getter with public access so your second class can access the field. Something like,

public JLabel getError() {
     return error;
}

Or (as @MadProgrammer suggested in the comments, a mutator) like

public void setError(String txt) {
     error.setText(txt);
}

Then modify your second class, and pass the instance of GUI to it in the constructor. Like,

public class guessHandler implements ActionListener{
    private GUI gui;
    public guessHandler(GUI gui) {
        this.gui = gui;
    }
    public void actionPerformed(ActionEvent e) {
        gui.changePOS(4, 50, 0, 300, 20);
        gui.setError("HI from guessHandler.java");
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!