62,621
社区成员
发帖
与我相关
我的任务
分享
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();
}
}