Java 我可以与来自不同类的循环创建的按钮交互吗?
下面列出的两个方法来自两个独立的类,一个用于构建GUI,另一个用于处理游戏逻辑(根据请求)。我的问题是,我是否能够与另一个类在for循环中创建的按钮交互Java 我可以与来自不同类的循环创建的按钮交互吗?,java,user-interface,Java,User Interface,下面列出的两个方法来自两个独立的类,一个用于构建GUI,另一个用于处理游戏逻辑(根据请求)。我的问题是,我是否能够与另一个类在for循环中创建的按钮交互 public void colorChange() { if (play() == true){ button.setBackground(Color.red); } else { button.setBackground(Color.black); } } public JPa
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;
}
public void colorChange()
{
如果(play()==true){
按钮。背景(颜色。红色);
}
否则{
按钮。背景(颜色。黑色);
}
}
公共JPanel makeInnerJPanel()
{
JPanel-inner=新的JPanel((新的网格布局(5,5));
字符串[]项=新字符串[]{“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、,
"","","","","","",""};
对于(int i=0;i您可以公开变量
public JButton[] button;
从另一个类访问它们
CLASSNAME.button;
你是说你是否可以从actionPerformed()
调用colorChange()
?你试过了吗?会发生什么?