【发布时间】:2018-09-28 20:39:38
【问题描述】:
我一直收到同样的错误:“cBtn 无法解析为变量”
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Butt1");
JButton btn = new JButton("Change color");
frame.add(btn);
btn.setBackground(cBtn);
frame.setSize(250, 100);
frame.setVisible(true);
}
Color[] colors = new Color[] { Color.red, Color.orange, Color.yellow, Color.green, Color.blue,
new Color(075, 000, 130), new Color(128, 000, 128)};
int color;
public void actionPerformed(ActionEvent e) {
Color cBtn = (colors[color++ % colors.length]);
}
}
提前致谢!
【问题讨论】:
-
cBtn是一个局部变量 -> 只能在actionPerformed方法中访问。如果您想从其他方法访问它,请将其设为字段,或将其作为参数传递(例如,如果您正在处理摆动覆盖,则不可能)
标签: java colors click jbutton actionlistener