import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Sample extends Applet
{
final JButton b = new JButton("换颜色");
int i = 0;
boolean c = false;
public void init()
{
this.setSize(500, 500);
add(b);
final Color c[] = {Color.red, Color.orange, Color.yellow, Color.green, Color.black, Color.blue, Color.gray};
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(i == 6)
{
i = 0;
}
else
{
i++;
}
Graphics g1 = getGraphics();
g1.setColor(c[i]);
update(g1);
}
});
}
public void paint(Graphics g)
{
super.paint(g);
if(!c)
{
g.setColor(Color.red);
c = true;
}
g.fillOval(50, 50, 400, 400);
}
public static void main(String[] args)
{
new Sample().start();
}
}
|
请发表评论