java 字体从下往上循环播放

来源:互联网 发布:知乎 短句 编辑:程序博客网 时间:2024/04/29 23:07


主题:字体从下往上循环播放


java 代码如下:

import java.awt.*;import javax.swing.*;import java.util.*;import javax.swing.JPanel;public class RollingUpDown3 extends JFrame{HeadlinePanel news = new HeadlinePanel();    public RollingUpDown3() {        super("字体从下往上滚动");        setSize(500, 500);//设置窗口大小        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭按钮响应方式        JPanel pane = new JPanel();        pane.setLayout(new GridLayout(1, 1, 15, 15));//设置布局        pane.add(news);        setContentPane(pane);        show();        news.scroll();    }    public static void main(String[] arguments) {    RollingUpDown3 head = new RollingUpDown3();    }}class HeadlinePanel extends JPanel {    String[] headlines = { "         见","        惊 艳","       目 流 连","      再 难 思 迁","     踌 躇 欲 向 前","    只 恐 天 上 人 间","   悲 欢 喜 怒 一 线 牵","  循 环 往 复 恨 此 心 坚"," 花 开 花 落 转 眼 已 三 年","天 人 合 一 处 垂 首 对 漪 涟"," 思 或 淡 情 未 移 口 三 缄","  燕 去 燕 归 沧 海 桑 田","  倘 注 定 有 分 无 缘","   亦 感 蒙 赐 初 面","    纵 此 生 不 见","     平 安 惟 愿","      若 得 闲","       仍 念","        歉"    };   int height = 500;    int y = height-40;    void scroll() {        while (true) {            y = y - 1;            if (y < -headlines.length*20)//如果到了窗口底部                y = height-40;            repaint();//重绘窗口            try {                Thread.sleep(50);            } catch (InterruptedException e) { }        }    }    public void paintComponent(Graphics comp) {        Graphics2D comp2D = (Graphics2D)comp;        Font type = new Font("楷体", Font.BOLD, 20);//字体对象        GradientPaint gp=new GradientPaint(0,0,Color.blue,0,getSize().height,Color.white,false);//背景颜色渐变(蓝-->白)        comp2D.setFont(type);//设置字体        comp2D.setPaint(gp);        GradientPaint gp2=new GradientPaint(0,0,Color.red,0,getSize().height,Color.red,false);//字体颜色渐变(红-->粉)        comp2D.fillRect(0, 0, getSize().width, getSize().height);        comp2D.setPaint(gp2);        for (int i = 0; i < headlines.length; i++)//设置每一行字的位置            comp2D.drawString(headlines[i], 100, y + (20 * i));    }}


运行结果: