我一直在互联网上搜索,试图找出在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));
}
}
}
以下是我手头的一些旧代码,可能会有所帮助:
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();
}
}