2012-04-05 63 views
1

我想知道如何制作滚动文字。就像可以从右侧滚动到左侧的文本一样。如何在Java GUI中动画文本?动画/滚动文字

+0

AFAIK,Java不一定是设计用来制作文字动画的。你可以做的是将'AffineTransforms'应用到'Label',但我真的怀疑它看起来不错。也许有一些库支持这种东西。 – posdef 2012-04-05 12:14:23

+0

http://www.java2s.com/Code/Java/Threads/Swingandthreadsscrolltext.htm – 2012-04-05 12:20:07

+0

您想在JTextField中,JLabel中移动文本,还是移动文本框/标签,或在面板上移动文本?在最后一种情况下:graphics.drawString是你需要的。 – 2012-04-05 12:24:16

回答

5

也许不是OP的答案,但我看不到的原因,通过工具Swing Timer很简单,(可能是半透明的容器),并把那里JLabel,(更新到JLabel可能是从CharsArray到避免调整容器大小),例如

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JWindow; 
import javax.swing.Timer; 

public class SlideTextSwing { 

    private JWindow window = new JWindow(); 
    private JLabel label = new JLabel("Slide Text Swing, Slide Text Swing, .........."); 
    private JPanel windowContents = new JPanel(); 

    public SlideTextSwing() { 
     windowContents.add(label); 
     window.add(windowContents); 
     window.pack(); 
     window.setLocationRelativeTo(null); 
     final int desiredWidth = window.getWidth(); 
     window.getContentPane().setLayout(null); 
     window.setSize(0, window.getHeight()); 
     window.setVisible(true); 
     Timer timer = new Timer(20, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       int newWidth = Math.min(window.getWidth() + 1, desiredWidth); 
       window.setSize(newWidth, window.getHeight()); 
       windowContents.setLocation(newWidth - desiredWidth, 0); 
       if (newWidth >= desiredWidth) { 
        ((Timer) e.getSource()).stop(); 
        label.setForeground(Color.red); 
        mainKill(); 
       } 
      } 
     }); 
     timer.start(); 
    } 

    public void mainKill() { 
     Timer timer = new Timer(500, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.exit(0); 
      } 
     }); 
     timer.start(); 
    } 

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

      public void run() { 
       SlideTextSwing windowTest = new SlideTextSwing(); 
      } 
     }); 
    } 
} 
+1

显示了几个相关的方法[这里](http://stackoverflow.com/q/3617326/230513)。 – trashgod 2012-04-05 12:48:03

+0

感谢您编辑我的问题。 – 2012-04-14 07:23:17

+0

感谢您的语法,它是帮助我。但是,我想用GUI Java在JLabel中进行滚动。 – 2012-04-14 07:45:06