点击按钮改变背景颜色

liuwei_IT_love 2009-11-12 11:01:30
在容器中放置一个按钮,点击按钮改变背景颜色。
请问各位大侠:怎样实现循环变换5种颜色?请明示,谢谢
...全文
293 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
swandragon 2009-11-12
  • 打赏
  • 举报
回复

public class Test extends JFrame implements ActionListener {
Color colors[] = {Color.RED,Color.ORANGE,Color.YELLOW,Color.GREEN,Color.BLUE,Color.MAGENTA};
private int index = 0;
private JButton jbutton = new JButton("变换背景");
public Test(){
this.setLayout(null);
jbutton.setBounds(150,0,100,50);
this.add(jbutton);
this.setBounds(400,300,400,300);
jbutton.addActionListener(this);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e){
this.getContentPane().setBackground(colors[index++]);
if(index == colors.length)
index = 0;
}
public static void main(String[] args) throws Exception {
new Test();
}
}

junjun1984 2009-11-12
  • 打赏
  • 举报
回复
设个FLAG,每次按按钮就给FLAG设值。根据不同的值取颜色。
qingzhe2008 2009-11-12
  • 打赏
  • 举报
回复
楼上的不错

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧