Java 制作动画或';滚动';JFrame中向上的文本

Java 制作动画或';滚动';JFrame中向上的文本,java,animation,jframe,jlabel,marquee,Java,Animation,Jframe,Jlabel,Marquee,我一直在浏览互联网,试图找出在jframe中向上动画文本的最佳方法。我故意避免使用“滚动”这个词,因为这个词可能意味着完全不同的东西,但“滚动”动画正是我想要的。我对Java相当陌生,我还在学习诀窍。请帮我指出正确的方向 我一直在使用一个我从另一个问题中找到的字幕面板。它向左滚动,我不知道如何从底部向上移动它。再说一次,我希望被指向正确的方向!谢谢各位 package jframes; import java.awt.EventQueue; import java.awt.Font; impo

我一直在浏览互联网,试图找出在jframe中向上动画文本的最佳方法。我故意避免使用“滚动”这个词,因为这个词可能意味着完全不同的东西,但“滚动”动画正是我想要的。我对Java相当陌生,我还在学习诀窍。请帮我指出正确的方向

我一直在使用一个我从另一个问题中找到的字幕面板。它向左滚动,我不知道如何从底部向上移动它。再说一次,我希望被指向正确的方向!谢谢各位

package jframes;

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.SwingConstants;
import javax.swing.Timer;

public class Marquee extends JFrame {
    private static final long serialVersionUID = 1L;

public void display() {
    MarqueePanel mp = new MarqueePanel("string", 32);
    setTitle("Text JFrame");
    JFrame jframe = new JFrame();

    jframe.add(mp);

    jframe.setSize(500, 500);

    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.setVisible(true);
    mp.start();

}

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

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

class MarqueePanel extends JPanel implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    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 + 1;
        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) {
        // TODO Auto-generated method stub
        index++;
        if (index > s.length() - n) {
            index = 0;
        }
        label.setText(s.substring(index, index + n));
    }
}
}
jframes包;
导入java.awt.EventQueue;
导入java.awt.Font;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.SwingConstants;
导入javax.swing.Timer;
公共类选框扩展JFrame{
私有静态最终长serialVersionUID=1L;
公共空间显示(){
MarquePanel mp=新的MarquePanel(“字符串”,32);
setTitle(“文本框架”);
JFrame JFrame=新JFrame();
jframe.add(mp);
jframe.setSize(500500);
jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);
jframe.setVisible(true);
mp.start();
}
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
新选框().display();
}
});
}
类MarquePanel扩展了JPanel实现ActionListener{
/**
* 
*/
私有静态最终长serialVersionUID=1L;
私人静态最终积分率=12;
专用最终定时器=新定时器(1000/速率,此值);
专用最终JLabel标签=新JLabel();
私有最终字符串s;
私人终审法院;
私有整数索引;
公共MarquePanel(字符串s,整数n){
如果(s==null | | n<1){
抛出新的IllegalArgumentException(“空字符串或n<1”);
}
StringBuilder sb=新的StringBuilder(n);
对于(int i=0;is.长度()-n){
指数=0;
}
setText(s.substring(index,index+n));
}
}
}

以下是我的一些旧代码,可能会有所帮助:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class ScrollingScrollPane extends JScrollPane implements ActionListener
{
    private int scrollOffset;
    private Timer timer;
    private boolean firstTime = true;
    private int locationY;

    public ScrollingScrollPane(JComponent component, int delay, int scrollOffset)
    {
        super(component);

        this.scrollOffset = scrollOffset;

        setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

        component.setVisible( false );
        component.setSize( component.getPreferredSize() );
        getViewport().setBackground( component.getBackground() );
        getViewport().setLayout( null );

        timer = new Timer(delay, this);
    }

    public void startScrolling()
    {
        locationY = getViewport().getExtentSize().height;
        JComponent component = (JComponent)getViewport().getView();
        component.setVisible( true );
        component.setLocation(0, locationY);
        timer.start();
    }

    public void actionPerformed(ActionEvent e)
    {
        JComponent component = (JComponent)getViewport().getView();
        locationY -= scrollOffset;
        Dimension d = getViewport().getExtentSize();
        component.setBounds(0, locationY, d.width, d.height);
//      component.setLocation(0, locationY);
//      component.setSize( getViewport().getExtentSize() );
//      System.out.println(locationY);

        if (component.getPreferredSize().height + locationY < 0)
            timer.stop();
    }

    public static void main(String[] args)
    {
        JTextPane textPane = new JTextPane();
        textPane.setEditable(false);
        StyledDocument doc = textPane.getStyledDocument();

        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
        doc.setParagraphAttributes(0, 0, center, false);

        SimpleAttributeSet keyWord = new SimpleAttributeSet();
        StyleConstants.setFontSize(keyWord, 16);
        StyleConstants.setBold(keyWord, true);

        SimpleAttributeSet red = new SimpleAttributeSet();
        StyleConstants.setForeground(red, Color.RED);

        try
        {
            doc.insertString(doc.getLength(), "In a Galaxy", keyWord );
            doc.insertString(doc.getLength(), "\n", null );
            doc.insertString(doc.getLength(), "\nfar", red );
            doc.insertString(doc.getLength(), "\n", null );
            doc.insertString(doc.getLength(), "\nfar", red );
            doc.insertString(doc.getLength(), "\n", null );
            doc.insertString(doc.getLength(), "\naway", red );
            doc.insertString(doc.getLength(), "\n", null );
            doc.insertString(doc.getLength(), "\n...", null );
        }
        catch(Exception e) {}


        ScrollingScrollPane scrollPane = new ScrollingScrollPane(textPane, 50, 1);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.getContentPane().add( scrollPane );
//      frame.setResizable( false );
        frame.setSize(300, 300);
        frame.setVisible(true);

        scrollPane.startScrolling();
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.text.*;
公共类ScrollingScrollPane扩展JScrollPane实现ActionListener
{
私有整数偏移;
私人定时器;
private boolean firstTime=true;
私人国际地点;
公共ScrollingScrollPane(JComponent组件、int延迟、int scrollOffset)
{
超级(组件);
this.scrollOffset=scrollOffset;
setHorizontalScrollBarPolicy(JScrollPane.HorizontalScrollBar\uNever);
setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u NEVER);
组件。setVisible(false);
setSize(component.getPreferredSize());
getViewport().setBackground(component.getBackground());
getViewport().setLayout(空);
定时器=新定时器(延迟,此);
}
公共无效开始滚动()
{
locationY=getViewport().getExtentSize().height;
JComponent组件=(JComponent)getViewport().getView();
组件。setVisible(true);
组件设置位置(0,位置Y);
timer.start();
}
已执行的公共无效操作(操作事件e)
{
JComponent组件=(JComponent)getViewport().getView();
位置Y-=滚动偏移量;
维度d=getViewport().getExtentSize();
构件立根(0,位置,d.宽度,d.高度);
//组件设置位置(0,位置Y);
//setSize(getViewport().getExtentSize());
//系统输出打印LN(位置Y);
if(component.getPreferredSize().height+locationY<0)
timer.stop();
}
公共静态void main(字符串[]args)
{
JTextPane textPane=新的JTextPane();
textPane.setEditable(false);
StyledDocument doc=textPane.getStyledDocument();
SimpleAttributeSet center=新的SimpleAttributeSet();
StyleConstants.setAlignment(中心,StyleConstants.ALIGN\u中心);
doc.setParagraphAttributes(0,0,居中,false);
SimpleAttributeSet关键字=新建SimpleAttributeSet();
setFontSize(关键字,16);
StyleConstants.setBold(关键字,true);
SimpleAttributeSet红色=新的SimpleAttributeSet();
设置前景(红色,彩色,红色);
尝试
{
doc.insertString(doc.getLength(),“在银河系中”,关键字);
doc.insertString(doc.getLength(),“\n”,null);
doc.insertString(doc.getLength(),“\nfar”,红色);
doc.insertString(doc.getLength(),“\n”,null);
doc.insertString(doc.getLength(),“\nfar”,红色);
doc.insertString(doc.getLength(),“\n”,null);
doc.insertString(doc.getLength(),“\naway”,红色);
doc.insertString(doc.getLength(),“\n”,null);
doc.insertString(doc.getLength(),“\n…”,null);
}
捕获(例外e){}
ScrollingScrollPane scrollPane=新的ScrollingScrollPane(文本窗格,50,1);
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(滚动窗格);
//帧。可设置大小(fa