wroll中的JScrollpane中的ScrollBar运动不

wroll中的JScrollpane中的ScrollBar运动不

本文介绍了Swroll中的JScrollpane中的ScrollBar运动不平滑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的情况。
我有滚动窗格,其viewportView是JPanel
而且JPanel的布局为BoxLayout。在这个面板中,我添加了一个扩展JPanel的类,该类包含JComponents。

I have a situation like this.I have scrollpane whose viewportView is a JPanelAnd that JPanel has the layout as BoxLayout. In this panel I add one class which extends JPanel and that class contains JComponents.

因此,在运行应用程序时,JComponents显示在JScrollPane中。
这就是我的ScrollPane的形成方式。

So while running an application, the JComponents are shown in the JScrollPane.This is how my ScrollPane is formed.

这里的问题是,当数据超过750行以上时,滚动条开始出现问题。
当用鼠标滚轮向上或向下滚动时,滚动不会平滑移动,它突然停在中间并再次开始,说它有一个生涩的运动。
我的问题是如何在这种情况下获得平稳的鼠标移动。

The problem here is, When the data exceeds more than around 750 rows, The scrollbar starts giving problems.When scrolling up or down by mouse wheel, scroll doesnot move smoothly, It suddenly stops in the middle and again starts, say it has a jerky movement.my Question is how can i get the smooth mouse movement in this scenario.

我的scrollPane就像这样

My scrollPane is like this

public JScrollPane getScrollPane() {
    if (scrollPane == null) {
        scrollPane = new JScrollPane();
        scrollPane.setSize(new Dimension(1000, 433));
        scrollPane.setLocation(new Point(10, 10));
        scrollPane.setColumnHeaderView(getHeaderOfRowPanel());
        scrollPane
                .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setViewportView(getScrollPanel());
        scrollPane
                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setUnitIncrement(
                unitIncrement);

    }
    return scrollPane;
}

private JPanel getScrollPanel() {
    if (scrollPanel == null) {
        scrollPanel = new JPanel();
        scrollPanel.setBorder(null);
        scrollPanel.setLayout(new BoxLayout(getScrollPanel(),
                BoxLayout.Y_AXIS));
    }
    return scrollPanel;
}


private class RowPanel extends JPanel {
//My components are here ..
//I add this Panel in scrollPanel
}


推荐答案

看看,因为一堆 JScollPane 中的JPanels JList 相比具有un_natural滚动, JTable JTextArea

Have look at JScrollBar.setUnitIncrement, beacuse bunch of JPanels in the JScollPane has un_natural scrolling in compare with JList, JTable or JTextArea

示例

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

public class JScrollBarUnitIncrement {

    public static void main(String[] args) {
        final JFrame f = new JFrame("");
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2000, 1));
        for (int i = 0; i != 2000; i++) {
            JButton btn = new JButton("Button 2");
            panel.add(btn);
        }
        final JScrollPane sPane = new JScrollPane(panel);
        final int increment = 50;
        sPane.getVerticalScrollBar().setUnitIncrement(increment);
        KeyStroke kUp = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
        KeyStroke kDown = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
        sPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(kUp, "actionWhenKeyUp");
        sPane.getActionMap().put("actionWhenKeyUp", new AbstractAction("keyUpAction") {

            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                final JScrollBar bar = sPane.getVerticalScrollBar();
                int currentValue = bar.getValue();
                bar.setValue(currentValue - increment);
            }
        });
        sPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(kDown, "actionWhenKeyDown");
        sPane.getActionMap().put("actionWhenKeyDown", new AbstractAction("keyDownAction") {

            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                final JScrollBar bar = sPane.getVerticalScrollBar();
                int currentValue = bar.getValue();
                bar.setValue(currentValue + increment);
            }
        });
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(sPane);
        f.pack();
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                f.setVisible(true);
            }
        });
    }

    private JScrollBarUnitIncrement() {
    }
}

这篇关于Swroll中的JScrollpane中的ScrollBar运动不平滑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 14:23