有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing用Java创建动画JScrollPane的简单方法?

我现在有一个JScrollPane,它基本上是一个项目列表。此JScrollPane将显示在信息屏幕上

我要寻找的是一种方法,可以让它在给定的时间间隔内自动滚动到列表的底部,然后返回顶部。我认识到,使用JScrollPane可能无法实现这一点,因此任何关于替代方案的建议都将非常好


共 (2) 个答案

  1. # 1 楼答案

    你有没有考虑过使用定时器每隔一段时间向JScrollPane发送滚动指令?只是我想到的第一件事

  2. # 2 楼答案

    通常我会用TimingFramework或者你可以用Trident或者Unviversal Tween Engine作为任何动画的基础。主要原因是,除了为您完成大部分工作外,它们还提供可变速度,这将使动画看起来更自然

    但是您可以使用javax.swing.Timer实现基本概念

    此示例将允许您滚动到图像底部,然后再返回

    动画将花费5秒(由runningTiming变量提供),允许其可变(图像越大,移动越快,图像越小,移动越慢)

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class AutoScroller {
    
        public static void main(String[] args) {
            new AutoScroller();
        }
        private long startTime = -1;
        private int range = 0;
        private int runningTime = 5000;
        private int direction = 1;
    
        public AutoScroller() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    final JScrollPane scrollPane = new JScrollPane(new TestPane());
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(scrollPane);
    //                frame.pack();
                    frame.setSize(scrollPane.getPreferredSize().width, 200);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                    Timer timer = new Timer(40, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            if (startTime < 0) {
                                startTime = System.currentTimeMillis();
                                range = scrollPane.getViewport().getView().getPreferredSize().height - scrollPane.getHeight();
                            }
                            long duration = System.currentTimeMillis() - startTime;
                            float progress = 1f;
                            if (duration >= runningTime) {
                                startTime = -1;
                                direction *= -1;
                                // Make the progress equal the maximum range for the new direction
                                // This will prevent it from "bouncing"
                                if (direction < 0) {
                                    progress = 1f;
                                } else {
                                    progress = 0f;
                                }
                            } else {
                                progress = (float) duration / (float) runningTime;
                                if (direction < 0) {
                                    progress = 1f - progress;
                                }
                            }
    
                            int yPos = (int) (range * progress);
    
                            scrollPane.getViewport().setViewPosition(new Point(0, yPos));
    
                        }
                    });
                    timer.setRepeats(true);
                    timer.setCoalesce(true);
                    timer.start();
    
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private BufferedImage image;
    
            public TestPane() {
                try {
                    image = ImageIO.read(new File("Path/to/your/image"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
            @Override
            public Dimension getPreferredSize() {
                return image == null ? new Dimension(200, 200) : new Dimension(image.getWidth(), image.getHeight());
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (image != null) {
                    Graphics2D g2d = (Graphics2D) g.create();
                    int x = (getWidth() - image.getWidth()) / 2;
                    int y = (getHeight() - image.getHeight()) / 2;
                    g2d.drawImage(image, x, y, this);
                    g2d.dispose();
                }
            }
        }
    }