Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何提高JScrollPane上的慢速滚动速度?_Java_Swing_Jpanel_Jscrollpane_Mousewheel - Fatal编程技术网

Java 如何提高JScrollPane上的慢速滚动速度?

Java 如何提高JScrollPane上的慢速滚动速度?,java,swing,jpanel,jscrollpane,mousewheel,Java,Swing,Jpanel,Jscrollpane,Mousewheel,我正在项目中的JScrollPane中添加JPanel 一切正常,但在JPanel中使用鼠标滚轮时存在一个问题。它的滚动速度非常慢。如何使它更快 我的代码是: JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); objCheckBoxList = new CheckBoxList(); BaseTreeExplorer node = (BaseTreeExplorer)projectMain.objCommon.t

我正在项目中的
JScrollPane
中添加
JPanel

一切正常,但在JPanel中使用鼠标滚轮时存在一个问题。它的滚动速度非常慢。如何使它更快

我的代码是:

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());
objCheckBoxList = new CheckBoxList();
BaseTreeExplorer node = (BaseTreeExplorer)projectMain.objCommon.tree.getLastSelectedPathComponent();
if (node.getObject() != null) {
    cmbList.setSelectedItem(node.getParent().toString());
} else {
    if (node.toString().equalsIgnoreCase("List of attributes")) {
        cmbList.setSelectedIndex(0);
    } else {
        cmbList.setSelectedItem(node.toString());
    }
}

panel.add(objCheckBoxList);

JScrollPane myScrollPanel = new JScrollPane(panel);

myScrollPanel.setPreferredSize(new Dimension(200, 200));
myScrollPanel.setBorder(BorderFactory.createTitledBorder("Attribute List"));

您可以使用这行代码设置滚动速度
myJScrollPane.getVerticalScrollBar().setUnitIncrement(16);

是详细信息。

之所以出现此错误,是因为swing以像素而不是文本行来解释滚动速度。如果您正在寻找可接受解决方案的更易访问的替代方案,则可以使用以下功能计算并设置实际所需的滚动速度(以像素为单位):

public static void fixScrolling(JScrollPane scrollpane) {
    JLabel systemLabel = new JLabel();
    FontMetrics metrics = systemLabel.getFontMetrics(systemLabel.getFont());
    int lineHeight = metrics.getHeight();
    int charWidth = metrics.getMaxAdvance();
            
    JScrollBar systemVBar = new JScrollBar(JScrollBar.VERTICAL);
    JScrollBar systemHBar = new JScrollBar(JScrollBar.HORIZONTAL);
    int verticalIncrement = systemVBar.getUnitIncrement();
    int horizontalIncrement = systemHBar.getUnitIncrement();
            
    scrollpane.getVerticalScrollBar().setUnitIncrement(lineHeight * verticalIncrement);
    scrollpane.getHorizontalScrollBar().setUnitIncrement(charWidth * horizontalIncrement);
}

请注意,当swing包含单个组件(如
JTable
JTextArea
)时,它可以正确计算滚动速度。此修复程序特别适用于滚动窗格包含
JPanel

的情况,感谢@mbaydar的回复。