我可以与来自不同类的循环创建的按钮进行交互吗?

时间:2016-03-30 23:17:02

标签: java user-interface

-->

下面列出的两种方法来自两个独立的类,一个用于构建GUI,另一个用于处理游戏逻辑(根据请求)。我的问题是我能够与其他类中的for循环中创建的按钮进行交互吗?

public void colorChange()
{
    if (play() == true){
        button.setBackground(Color.red);
    }
    else {
        button.setBackground(Color.black);
    }
}

public JPanel makeInnerJPanel()
{
    JPanel inner = new JPanel((new GridLayout(5, 5)));
    String[] items = new String[] {"","","","","","","","","","","","","","","","","","",
            "","","","","","",""};
    for (int i = 0; i<items.length; i++){
        JButton button = new JButton(items[i]);
        button.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e){

                }
            }
        );
        inner.add(button);
    }

    return inner;
}

1 个答案:

答案 0 :(得分:0)

您可以将变量公开

public JButton[] button;

从另一个类

访问它们
CLASSNAME.button;
相关问题
最新问题