java - 将这个 "Marquee code"添加到我的 Jframe 中的 JLabel

标签 java jpanel jlabel marquee netbeans-7.4

我在其他帖子上读过这段代码,它在带有 JPanel 和 JLabel 的单个 Jframe 上工作。现在,在这段代码的帮助下,我想将其应用到我的 JFrame 的 JLabel 中。 其中我的 JFrame 名称是 AddBatch,JPanel 是 pnl_addBatch,JLabel 是 lbl_addBatch [使用惰性拖放操作将所有内容设置到其位置:)]

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

最佳答案

Marquee 功能不必驻留在此 MarqueePanel 中。这可能只是作为一个例子。但是您可以更改该类,以便它在其构造函数中接受 JLabel(并且它不再扩展 JPanel)。这样,您可以将此选取框效果应用到任何 JLabel - 也可以应用到已经存在的 JLabel:

JLabel lbl_addBatch = createdSomewhere();

// Add marquee effect to the existing label:
Marquee marquee = new Marquee(lbl_addBatch, s, 32);
marquee.start();

相应地更改了代码:

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.";

        JLabel lbl_addBatch = new JLabel();
        JPanel pnl_addBatch = new JPanel();
        pnl_addBatch.add(lbl_addBatch);

        Marquee marquee = new Marquee(lbl_addBatch, s, 32);
        marquee.start();

        f.add(pnl_addBatch);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MarqueeTest().display();
            }
        });
    }
}

class Marquee implements ActionListener {

    private static final int RATE = 12;
    private final Timer timer = new Timer(1000 / RATE, this);
    private final JLabel label;
    private final String s;
    private final int n;
    private int index;

    public Marquee(JLabel label, 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.label = label;
        this.s = sb + s + sb;
        this.n = n;
        label.setFont(new Font("Serif", Font.ITALIC, 36));
        label.setText(sb.toString());
    }

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

关于java - 将这个 "Marquee code"添加到我的 Jframe 中的 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21589134/

相关文章:

java - 使用 ImageIcon 和 JLabel

java - 在 ArrayList 内的 LinkedHashMap 中查找和赋值的替代方法

java - Android快速位图绘制

java - 如何使用 Google Place API 或任何 JAVA API 从 URL 获取经度/纬度

java - JPanel 格式问题

java - 一框内有 2 个不同宽度的面板

Java堆叠组件

java - 在 arraylist for 循环中执行 Collections.swap() 是否安全?

java - 在 JFreeChart 中使用 JTextField 更新正态分布图

java - 将文本添加到另一个类的标签 - 简单的逻辑问题