2012-04-05 5 views
1

スクロールテキストを作成する方法について知りたいと思います。右から左にスクロールできるテキストのように。 Java GUIでテキストをアニメーション化する方法は?アニメーション/スクロールテキスト

+0

AFAIK、Javaは必ずしもテキストアニメーションを作成するように設計されているわけではありません。あなたができることは、 'AffineTransforms'を' Label'に適用することですが、私はそれがどんなに良く見えるのか本当に疑いがあります。おそらく、そのようなものをサポートするライブラリがあります。 – posdef

+0

http://www.java2s.com/Code/Java/Threads/Swingandthreadsscrolltext.htm –

+0

JTextFieldのテキストをJLabelで移動するか、Textfield/theラベルを移動するか、パネル上のテキストを移動しますか?最後のケースでは、graphics.drawStringが必要です。 –

答えて

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

+0

私の質問を編集していただきありがとうございます。 –

+0

構文をありがとう、それは私を助ける。しかし、私はGUIのJavaでJLabelでスクロールしたいです。 –