ScrollBar在Swing中的JScrollpane中移动不流畅

ScrollBar在Swing中的JScrollpane中移动不流畅,第1张

ScrollBar在Swing中的JScrollpane中移动不流畅

看一下JScrollBar.setUnitIncrement,因为与

JPanels
中的一堆
JScollPane
相比有不自然的滚动
JList
JTable
或者
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() {    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5094585.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存