在jframe中向上滚动或设置文本动画

z4bn682m  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(319)

我一直在搜索互联网,试图找出最好的方式来动画文本向上在一个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));
    }
}
}
5q4ezhmt

5q4ezhmt1#

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

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();
    }
}

相关问题