我正在开发的NetBeans平台(NetBeans模块)的桌面应用程序中,我有一个desktoppane和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 mosue轮事件?
我已经搜查,但我发现滚动面板的事件,但我明确地寻找滚动条..
我已删除了额外的代码,并显示什么,我在下面的示例代码寻找
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
}
}
我认为你已经看的AdjustmentListener而非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());
}
});
。
。
编辑
:-)没有,没有在我身边的任何问题:-)
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;
}
}
}
从输出的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)
假如你尝试添加setWheelScrollingEnabled(真/假) ,在你的JScrollPane中。
我实际的问题是,我没有得到改变了鼠标滚轮改变滚动的价值,但我是越来越鼠标滚轮事件,所以我必须解决如下:
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...
}