java 动态button

来源:百度知道 编辑:UC知道 时间:2024/06/23 04:36:55
我想根据按钮的背景色动态的改变按钮背景色
比如有button1,初始背景颜色设为红色,现在按一下按钮就要改变成黄色,再按一下就变成绿色,再按一下又变成红色,
就是在红、黄、绿三色之间轮流改变
我应该怎么做,能否给个实例?

Color [] color = new Color{Color.RED,Color.YELLOW,Color.BLUE};
int currentColor = -1;

在你的actionPerformed(ActionEvent e){
if(currentColor ==3){
currentColor = -1;
}
currentColor++;
然后就怎么变色会了吧
}

button1.setBackground(currentColor)