编写Applet,实现字体样式和页面背景颜色的设置

1.接受用户输入指定的字号,字体风格,在Applet上显示一段指定字体的文字;

2.接受用户输入的R,G,B三种颜色的分量,配置页面的背景颜色


public class AppletTest extends Applet{

      Font f;
      int style; //字体风格
      Color c;
      
   public void init() {
//接受用户对字体样式的设置       
       Scanner console=new Scanner(System.in);
       System.out.println("请输入Applet上显示的文字的字体:");
       String zt=console.next();
       System.out.println("对应的字体风格,斜体/粗体/普通体:");
       String zttype=console.next();
       if(zttype.equals("斜体")) 
           style=Font.ITALIC;
       else if(zttype.equals("粗体")) 
           style=Font.BOLD;
       else if(zttype.equals("普通体"))
           style=Font.PLAIN;
       System.out.println("对应的字号:");
       int zh=console.nextInt();
       f=new Font("zt", style, zh);
//接受用户对背景颜色的配置       
       System.out.println("请输入R,G,B,三种颜色的分量(0~255)");
       System.out.println("颜色分量对应的R值");
       int R=console.nextInt();
       System.out.println("颜色分量对应的G值");
       int G=console.nextInt();
       System.out.println("颜色分量对应的B值");
       int B=console.nextInt();
       c=new Color(R, G, B);
   }
//实现对字体样式和背景颜色的设置   
   public void paint(Graphics g) {
       String s="配置页面背景颜色和字体颜色的显示";
       g.setFont(f);
       g.drawString(s, 20, 20);
       setBackground(c);
   }
    
}
 


运行结果:

猜你喜欢

转载自blog.csdn.net/xxx_1_1/article/details/82497835
今日推荐