2012-04-05 50 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