我在我的JPanel中添加了一个自定义的mouseWheelListener,因为我将为这个使用ctrl鼠标轮的面板实现缩放功能。我的JPanel也在JScrollpane中。我的问题是;当我将MouseWheelListener添加到面板时,即使没有按下Ctrl,鼠标轮也不适用于scrollpane。我想要这样;鼠标滚轮在ctrl按下时用于缩放,如果没有按下,它应该适用于scrollpane。我该怎么做呢?
public class ZoomTest {
public static void main(String[] args) {
JPanel jZoomPanel = new JPanel();
jZoomPanel.setPreferredSize(new Dimension(300, 500));
jZoomPanel.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if(e.isControlDown()) {
// TODO implement zoom
}
}
});
JScrollPane jsc = new JScrollPane(jZoomPanel);
JFrame jDemoFrame = new JFrame();
jDemoFrame.getContentPane().add(jsc);
jDemoFrame.setSize(300, 300);
jDemoFrame.setVisible(true);
}
}
您应该像这样发送您的活动:
jZoomPanel.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if(e.isControlDown()) {
// TODO implement zoom
} else {
JPanel panel = (JPanel) me.getSource();
MouseEvent newMe = SwingUtilities.convertMouseEvent(panel, me, jsc);
jsc.dispatchEvent(me);
}
}
});