Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中,如何直接更改另一个类&x27;I'时的按钮和字符串;我在另一个班_Java_Class_Variables - Fatal编程技术网

在java中,如何直接更改另一个类&x27;I'时的按钮和字符串;我在另一个班

在java中,如何直接更改另一个类&x27;I'时的按钮和字符串;我在另一个班,java,class,variables,Java,Class,Variables,我有一个GridBagLayout GUI,它在0 0单元格中添加Board类obj,在1 0单元格中添加RightPanel obj。我想做的是让我的GUI类能够访问RightPanel的字符串和按钮。首先,我目前只是尝试更改字符串wName,但没有成功。请帮我找出我做错了什么。 注意:我知道这个问题对其他人来说可能听起来很奇怪,但我真的需要知道如何解决 在我的GUI中,我正在执行以下操作: public class TheraGUI extends JFrame { public R

我有一个GridBagLayout GUI,它在0 0单元格中添加Board类obj,在1 0单元格中添加RightPanel obj。我想做的是让我的GUI类能够访问RightPanel的字符串和按钮。首先,我目前只是尝试更改字符串wName,但没有成功。请帮我找出我做错了什么。 注意:我知道这个问题对其他人来说可能听起来很奇怪,但我真的需要知道如何解决

在我的GUI中,我正在执行以下操作:

public class TheraGUI extends JFrame {
    public RightPanel rpref;

//I add this so I can call right panel
    public TheraGUI(RightPanel rpref){
        this.rpref = rpref;
    }

    public TheraGUI(){
        Board board = new Board();
        Board bref = new Board(this);
        RightPanel rp = new RightPanel();
        this.rpref.wName = "Gel"
        this.setLayout(new GridBagLayout());
        this.setTitle("Thera");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.add(board);
        this.add(rp);
        this.pack();    
    }

}
在我的右侧面板中,我有以下内容:

public class RightPanel extends JPanel {
    private static final String imageFolderPath = "src/resources/images/";
    private Dimension dimension = new Dimension(200, 500);
    private static final String defaultImgPiece = "demigod.png";
    public JLabel wImgPic;
    public String wName;

//And here I instantiate the constructor
    public TheraGUI guiref = new TheraGUI(this);    


public RightPanel(){
    this.setPreferredSize(dimension);
    this.setLayout(new GridBagLayout());
    GridBagConstraints rpc = new GridBagConstraints();

    //White's Panel
    JPanel wpanel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    rpc.gridx = 0;
    rpc.gridy = 0;
    this.add(wpanel, rpc);

//   Piece Name Info   //
    wName = "Helios, the Demigod";
    JLabel wNamePiece = new JLabel(wName);
    wNamePiece.setFont(new Font("Georgia", Font.BOLD, 16));
    wNamePiece.setForeground(Color.BLACK);
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL; 
    gbc.insets = new Insets(0, 20, 0, 20);
    wpanel.add(wNamePiece, gbc);

}

}

您的代码肯定需要一些结构来消除不受控制的耦合

除此之外,更改wName没有帮助,您需要调用wNamePiece.setText()。为此,需要将其作为成员变量

此外,您需要在事件调度器线程内进行此调用。如果您不是在单独的线程中进行此调用,那么您是安全的