applet程序,用按钮控制applet背景颜色随机的变化
1个回答
展开全部
这个挺简单的
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.util.Random;
public class ChangeBcColor extends Applet {
private static final long serialVersionUID = 1L;
private Button change;
private Random ran;
public void init(){
change=new Button("ChangeColor");
this.setSize(300, 200);
this.add(change);
change.addActionListener(new ButtonAction(this));
ran=new Random();
}
public void paint(Graphics g){
g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
public void ButtonActionPerformed(ActionEvent e){
if(e.getActionCommand().equals("ChangeColor")){
repaint();
}
}
}
class ButtonAction implements java.awt.event.ActionListener{
ChangeBcColor su;
public ButtonAction(ChangeBcColor bun){
this.su=bun;
}
@Override
public void actionPerformed(ActionEvent e) {
su.ButtonActionPerformed(e);
}
}
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.util.Random;
public class ChangeBcColor extends Applet {
private static final long serialVersionUID = 1L;
private Button change;
private Random ran;
public void init(){
change=new Button("ChangeColor");
this.setSize(300, 200);
this.add(change);
change.addActionListener(new ButtonAction(this));
ran=new Random();
}
public void paint(Graphics g){
g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
public void ButtonActionPerformed(ActionEvent e){
if(e.getActionCommand().equals("ChangeColor")){
repaint();
}
}
}
class ButtonAction implements java.awt.event.ActionListener{
ChangeBcColor su;
public ButtonAction(ChangeBcColor bun){
this.su=bun;
}
@Override
public void actionPerformed(ActionEvent e) {
su.ButtonActionPerformed(e);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询