编写一个Applet程序,显示一个字符串,用两个按钮控制字符串的放大和缩小
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
public class ColorString extends JFrame{
ColorString(){
super("显示彩色字符串的窗口");setBounds(200,200,500,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c= getContentPane();
MyCanvas1 c1=new MyCanvas1();
c1.setBackground(Color.green);
c.add(c1,BorderLayout.CENTER);
}
public static void main(String[] args) {
ColorString f = new ColorString();
}
}
class MyCanvas1 extends Canvas {
int red,green,blue;
char c[]={'一','个','彩','色','字','符','串'};
public void paint(Graphics g) {
g.setFont(new Font("宋体",0,40));
for (int i=0;i<7;i++) {
red=(int)(Math.random()*255);
green=(int)(Math.random()*255);
blue=(int)(Math.random()*255);
g.setColor(new Color(red,green,blue));
g.drawChars(c,i,1,20+i*40,80); }
}
}
附上 显示不同颜色的文字的程序代码