我在我的JPanel中添加了一个自定义mousewheellistener,因为我将为这个使用ctrl + mousewheel的面板实现缩放功能。我的JPanel也在JScrollpane中。我的问题是;当我将MouseWheelListener添加到面板时,即使未按下Ctrl键,鼠标滚轮也不适用于滚动窗格。我要那个;当按下ctrl时,鼠标滚轮可以进行缩放,如果没有按下,它应该适用于滚动窗格。我怎么能这样做?
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);
}
}
答案 0 :(得分:2)
您应该像这样派遣您的活动:
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);
}
}
});