如何使JScrollPane(在BorderLayout中,包含JPanel)顺利地自动滚动 [英] How to make JScrollPane (In BorderLayout, containing JPanel) smoothly autoscroll

查看:136
本文介绍了如何使JScrollPane(在BorderLayout中,包含JPanel)顺利地自动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JScrollPanel内部放置一个不同大小的JPanel(可能比标准屏幕宽得多)。目前它效果很好,我已经配置滚动条手动工作正常,但我希望JPanel不断向左滚动,以便随着时间的推移显示整个事物。我发现的所有答案都是特定于JTextArea并使用Carets,或使用rectToVisible。这些都不会起作用,因为我试图在内部滚动到一个JPanel。

I'm trying to have a JPanel of varying size (potentially much wider than the standard screen) inside of a JScrollPanel. Currently it works out great, and I have configured the scrollbars to work fine manually, however I would like the JPanel to "scroll" constantly to the left, so that over time the whole thing is displayed. All of the answers I found are specific to JTextArea and use Carets, or use rectToVisible. Neither of these will work because I'm trying to scroll internally to a single JPanel.

我已经包含了我认为是下面所有相关代码的内容。

I've included what I believe to be all of the relevant code below.

center是JPanel(其中Grid是一个子类,用于专门绘制一些特定单元格的网格),带有我想要自动滚动的BorderLayout。

center is the JPanel (of which Grid is a subclass, used to paint specifically a grid with some specific cells colored) with a BorderLayout that I would like to autoscroll.

public GuiViewFrame(Song playMe) {
  String[][] songArray = playMe.to2DArray();

  this.displayPanel = new ConcreteGuiViewPanel(playMe);
  main = new JPanel();
  main.setLayout(new BorderLayout());
  displayPanel.setLayout(new BorderLayout());
  center = new Grid(playMe);
  labels = new Labels(playMe);
  horiz = new Horiz(playMe);
  center.setPreferredSize(new Dimension(10 * songArray.length, 10 * songArray[0].length));
  horiz.setPreferredSize(new Dimension(10 * songArray.length, 10));
  horiz.setVisible(true);

  main.add(center, BorderLayout.CENTER);
  main.add(horiz, BorderLayout.NORTH);

  scroll = new JScrollPane(main,
        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  add(scroll, BorderLayout.CENTER);
  labels.setPreferredSize(new Dimension(20, 10 * songArray[0].length));
  labels.setVisible(true);
  add(labels, BorderLayout.WEST);

  JScrollBar horiz = scroll.getHorizontalScrollBar();
  InputMap im = horiz.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
  im.put(KeyStroke.getKeyStroke("RIGHT"), "positiveUnitIncrement");
  im.put(KeyStroke.getKeyStroke("LEFT"), "negativeUnitIncrement");
  im.put(KeyStroke.getKeyStroke("HOME"), "minScroll");
  im.put(KeyStroke.getKeyStroke("END"), "maxScroll");

  this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  this.pack();
}

整个项目是为播放组合MIDI的音乐生成视图和GUI,但是现在一旦MIDI播放了足够的歌曲,相关的音符就会关闭。我想以一个速率滚动以跟上MIDI的步伐。

The project as a whole is to generate a view for playing music that combines MIDI and a GUI, but right now once MIDI plays enough of the song, the relevant notes are off screen. I would like to scroll at a rate to keep pace with MIDI.

推荐答案

您可以设置水平滚动条的值来控制当前可见的内容:

You can set the value of the horizontal scrollbar to control what is currently visible:

JScrollBar horizontal = scroll.getHorizontalScrollBar();
horizontal.setValue( horizontal.getValue() + ??? );

您需要使用 Swing Timer 以适当的间隔安排滚动。

You would need to use a Swing Timer to schedule the scrolling at an appropriate interval.

简单示例使用计时器滚动文本:

Simple example of using a Timer to scroll text:

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

public class TimerTest extends JPanel implements ActionListener
{
    JLabel timeLabel;
    JLabel scrollLabel;

    public TimerTest()
    {
        setLayout( new BorderLayout() );

        timeLabel = new JLabel( new Date().toString() );
        add(timeLabel, BorderLayout.NORTH);

        scrollLabel = new JLabel( "Some continuously scrolling text!!      " );
        add(scrollLabel, BorderLayout.SOUTH);

        int time = 1000;
        javax.swing.Timer timer = new javax.swing.Timer(time, this);
        timer.setInitialDelay(1);
        timer.start();
    }

    public void actionPerformed(ActionEvent e)
    {
        timeLabel.setText( new Date().toString() );
        String oldText = scrollLabel.getText();

        // Scroll right to left
        String newText = oldText.substring(1) + oldText.substring(0, 1);

        // Scroll left to right
//      int length = oldText.length();
//      String newText = oldText.substring(length-1, length)
//          + oldText.substring(0, length-1);

        scrollLabel.setText( newText );
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TimerTest() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

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

这篇关于如何使JScrollPane(在BorderLayout中,包含JPanel)顺利地自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆