3

我正在 netbeans 平台(netbeans 模块)中开发一个桌面应用程序,其中我有一个桌面窗格和一个 jscrollbar。我已经实现了 MouseWheelListener并添加了

scrollBar.addMouseWheelListener(this);

在类的构造函数中。现在,当我滚动鼠标滚轮时,它不会滚动滚动条,尽管我在

 private void scrollBarMouseWheelMoved(java.awt.event.MouseWheelEvent evt) { 


 System.out.println("mouse value is------------ " + evt.paramString());
}

上面 sout 的输出是

mouse value is------------ MOUSE_WHEEL,(8,49),absolute(0,0),button=0,clickCount=0,scrollType=WHEEL_UNIT_SCROLL,scrollAmount=3,wheelRotation=1

我现在应该怎么做才能在 jscrollbar 上启用鼠标滚轮事件?

我已经搜索过,但我找到了滚动窗格的事件,但我正在明确寻找滚动条..

我已经删除了额外的代码,并在下面的示例代码中显示了我正在寻找的内容

    public final class ScrollableWindow1TopComponent extends TopComponent implements ComponentListener, MouseWheelListener {

    private javax.swing.JScrollBar scrollBar;
    private javax.swing.JDesktopPane scrollableGraphnewContainer;

    public ScrollableWindow1TopComponent() {
      this.addComponentListener(this);
      scrollBar.addMouseWheelListener(this);

    }
     private void scrollBarMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {                                          
       System.out.println("mouse value is------------ " + evt.paramString());
    }
     private void scrollBarAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) { 
      //code that works fine 
    }
  }
4

3 回答 3

7

I think that you have look at AdjustmentListener rather than MouseXxxListener,

final JScrollBar scrollBar = myScrollpane.getVerticalScrollBar();
scrollBar.addAdjustmentListener(new AdjustmentListener() {

    @Override
    public void adjustmentValueChanged(AdjustmentEvent e) {
        System.out.println("JScrollBar's current value = " + scrollBar.getValue());
    }
});

.

.

EDIT

:-) no, without any issue on my side :-)

enter image description here

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

public class ListPanel extends JFrame {

    private static final long serialVersionUID = 1L;

    public ListPanel() {
        setLayout(new GridLayout(0, 2, 10, 10));
        DefaultListModel model = new DefaultListModel();
        model.addElement(createButtons("one"));
        model.addElement(createButtons("two"));
        model.addElement(createButtons("three"));
        model.addElement(createButtons("four"));
        model.addElement(createButtons("five"));
        model.addElement(createButtons("six"));
        model.addElement(createButtons("seven"));
        model.addElement(createButtons("eight"));
        model.addElement(createButtons("nine"));
        model.addElement(createButtons("ten"));
        model.addElement(createButtons("eleven"));
        model.addElement(createButtons("twelwe"));
        JList list = new JList(model);
        list.setCellRenderer(new PanelRenderer());
        JScrollPane scroll1 = new JScrollPane(list);
        final JScrollBar scrollBar = scroll1.getVerticalScrollBar();
        scrollBar.addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                System.out.println("JScrollBar's current value = " + scrollBar.getValue());
            }
        });
        add(scroll1);
        JScrollPane scroll2 = new JScrollPane(createPanel());
        add(scroll2);        
        final JScrollBar scrollBar1 = scroll2.getVerticalScrollBar();
        scrollBar1.addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                System.out.println("JScrollBar's current value = " + scrollBar1.getValue());
            }
        });

    }

    public static JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 1, 1, 1));
        panel.add(createButtons("one"));
        panel.add(createButtons("two"));
        panel.add(createButtons("three"));
        panel.add(createButtons("four"));
        panel.add(createButtons("five"));
        panel.add(createButtons("six"));
        panel.add(createButtons("seven"));
        panel.add(createButtons("eight"));
        panel.add(createButtons("nine"));
        panel.add(createButtons("ten"));
        panel.add(createButtons("eleven"));
        panel.add(createButtons("twelwe"));
        return panel;
    }

    public static JButton createButtons(String text) {
        JButton button = new JButton(text);
        return button;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                ListPanel frame = new ListPanel();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                //frame.pack();
                frame.setSize(270, 200);
                frame.setVisible(true);
            }
        });
    }

    class PanelRenderer implements ListCellRenderer {

        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JButton renderer = (JButton) value;
            renderer.setBackground(isSelected ? Color.red : list.getBackground());
            return renderer;
        }
    }
}

output from System.out.println(...)

run:
JScrollBar's current value = 0
JScrollBar's current value = 0
JScrollBar's current value = 1
JScrollBar's current value = 2
JScrollBar's current value = 3
JScrollBar's current value = 4
JScrollBar's current value = 5
JScrollBar's current value = 6
JScrollBar's current value = 7
JScrollBar's current value = 8
JScrollBar's current value = 9
JScrollBar's current value = 10
JScrollBar's current value = 11
JScrollBar's current value = 12
JScrollBar's current value = 13
JScrollBar's current value = 14
JScrollBar's current value = 15
JScrollBar's current value = 78
JScrollBar's current value = 143
JScrollBar's current value = 78
JScrollBar's current value = 0
JScrollBar's current value = 78
JScrollBar's current value = 143
BUILD SUCCESSFUL (total time: 10 seconds)
于 2012-04-25T12:34:30.087 回答
2

您是否尝试在 JScrollPane 上添加setWheelScrollingEnabled(true/false) 。

于 2012-04-25T14:02:46.100 回答
1

我的实际问题是我没有在鼠标滚轮更改时更改滚动值,但我得到了鼠标滚轮事件,所以我解决了如下问题:

 private void scrollBarMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {                                          
        if (evt.getUnitsToScroll() > 0) {
            scrollBar.setValue(scrollBar.getValue() + 1);
        } else {
            scrollBar.setValue(scrollBar.getValue() - 1);
        }
   }      

每次我向上滚动滚轮时,evt.getUnitsToScroll() 给出正值,向下滚动它给出负值,所以我增加了滚动条的值,其余部分由自动处理

  private void scrollBarAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) { 
     //my code...
}
于 2012-04-26T07:28:18.260 回答