我在用 JB 做一个GUI的程序
frame2读取的是frame1的一个jlabel的值
但frame2上的那个的jlabel的长度有限(不能改变)
求一种能将frame1中字体一个一个显示出来达到滚动字幕的效果(就类似电视上屏幕下方从右到左出来字幕的那种效果)
(注:不是假滚动,网上有种通过timer类改变jlabel的x,y坐标的方法,我觉得这是一种假滚动)
Q 39163008 25小时在线求答案

解决方案 »

  1.   

    我自己已经解决了,现把代码贴出来,方便大家
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;
    import java.io.*;public class Test extends JPanel {
        private static final long serialVersionUID = 4767050156491994899L;
        private JLabel            label;
        private String            scrolledText;    public Test() {
            scrolledText = "滚动文字 Demo";
            label = new JLabel(new String(scrolledText));
            this.add(label);        Thread thread = new Thread(new TextChanger(label));
            thread.start();
        }    // @Override
        // protected void paintComponent(Graphics g) {
        // super.paintComponent(g);
        //
        // Graphics2D g2d = (Graphics2D) g;
        //
        // }    private static void createAndShowGUI() {
            JFrame frame = new JFrame("Frame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 400);        frame.getContentPane().add(new Test());        frame.setVisible(true);
        }    public static void main(String[] args) throws Exception {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    Test.createAndShowGUI();
                }
            });
        }
    }class TextChanger implements Runnable {
        private JLabel label;    public TextChanger(JLabel label) {
            this.label = label;
        }    public void run() {
            try {
                while (true) {
                    String text = label.getText();
                    if (text.length() > 1) {
                        text = text.substring(1, text.length()) + text.charAt(0);
                        label.setText(text);                    // Get the frame
                        Component frame = SwingUtilities.getRoot(label);
                        if (frame != null && (frame instanceof JFrame)) {
                            ((JFrame)frame).setTitle(text);
                        }                    label.repaint();
                    }                Thread.sleep(300);
                }
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
    不过我还有一个问题。就是JLabel显示字符多了的时候,会显示不出来,就会在后面
    显示"...",很不美观,怎么去掉?(不要改变JLabel大小)