SWING禁用或隐藏滚动条,但启用滚轮?

问题描述 投票:0回答:1

我想在JScrollPane中禁用或隐藏滚动条,但让用户使用鼠标滚轮向上和向下滚动。如果我使用verticalScrollBarPolicy(NEVER)它会隐藏滚动条,所以它没关系,但不能使用滚轮。

有什么建议?谢谢!

java swing jtable scrollbar
1个回答
1
投票

Based on this answer from CodeRanch,滚动条从未显示时禁用滚动。您需要覆盖JScrollBars isVisible属性以“欺骗”滚动。

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.Scrollable;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                JScrollPane scrollPane = new JScrollPane(new TestPane());
                JScrollBar scrollBar = new JScrollBar(JScrollBar.VERTICAL) {

                    @Override
                    public boolean isVisible() {
                        return true;
                    }
                };
                // if appropriate, uncomment
                //scrollBar.putClientProperty("JScrollBar.fastWheelScrolling", Boolean.TRUE);
                scrollPane.setVerticalScrollBar(scrollBar);
                scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
                frame.add(scrollPane);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel implements Scrollable {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 800);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            FontMetrics fm = g2d.getFontMetrics();
            Integer lineY = null;
            for (int yPos = fm.getAscent(); yPos < getHeight(); yPos += 30) {
                g2d.drawString(Integer.toString(yPos), 10, yPos + fm.getAscent());
                if (lineY != null) {
                    g2d.drawLine(15, lineY, 15, yPos);
                }
                lineY = yPos + fm.getAscent();
            }
        }

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(200, 200);
        }

        @Override
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 64;
        }

        @Override
        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 128;
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return false;
        }

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }

    }

}

你不需要使用Scrollable,它在这里纯粹用于演示目的来测试理论。

然而,这也停止调用getScrollableUnitIncrement,这可能会影响滚动的整体速度,你必须进一步调查

哦,还有其他注意事项,如果JScrollPane中包含的一个或多个组件注册到MouseWheelListener事件,它将阻止滚动窗格被通知

© www.soinside.com 2019 - 2024. All rights reserved.