java 字体从下往上循环播放


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


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));
    }

}


运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值