我正在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 wheel事件?
我搜索了但是我发现了滚动窗格的事件,但我正在寻找明确的滚动条..
我删除了额外的代码,并在以下示例代码
中显示了我要查找的内容 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
}
}
答案 0 :(得分:7)
我认为您已查看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)
答案 1 :(得分:2)
您是否尝试在JScrollPane上添加setWheelScrollingEnabled(true/false)。
答案 2 :(得分: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...
}