栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java Swing中的字幕效果

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Swing中的字幕效果

这是使用的示例

javax.swing.Timer

import java.awt.EventQueue;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.Jframe;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.Timer;public class MarqueeTest {    private void display() {        Jframe f = new Jframe("MarqueeTest");        f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        String s = "Tomorrow, and tomorrow, and tomorrow, "        + "creeps in this petty pace from day to day, "        + "to the last syllable of recorded time; ... "        + "It is a tale told by an idiot, full of "        + "sound and fury signifying nothing.";        MarqueePanel mp = new MarqueePanel(s, 32);        f.add(mp);        f.pack();        f.setLocationRelativeTo(null);        f.setVisible(true);        mp.start();    }    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     new MarqueeTest().display(); }        });    }}class MarqueePanel extends JPanel implements ActionListener {    private static final int RATE = 12;    private final Timer timer = new Timer(1000 / RATE, this);    private final JLabel label = new JLabel();    private final String s;    private final int n;    private int index;    public MarqueePanel(String s, int n) {        if (s == null || n < 1) { throw new IllegalArgumentException("Null string or n < 1");        }        StringBuilder sb = new StringBuilder(n);        for (int i = 0; i < n; i++) { sb.append(' ');        }        this.s = sb + s + sb;        this.n = n;        label.setFont(new Font("Serif", Font.ITALIC, 36));        label.setText(sb.toString());        this.add(label);    }    public void start() {        timer.start();    }    public void stop() {        timer.stop();    }    @Override    public void actionPerformed(ActionEvent e) {        index++;        if (index > s.length() - n) { index = 0;        }        label.setText(s.substring(index, index + n));    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/432040.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号