我正在開發一款遊戲,但是我發現我的MouseWheelListener
沒有工作。我簡化了代碼,使問題更清晰。我的MouseWheelListener不起作用
首先,在顯示窗口後,點擊Go In
。然後旋轉你的鼠標滾輪,什麼也沒有發生!我該如何解決這個問題?
爲了證明我沒有做出很愚蠢的錯誤,最小化和最大化你的窗口或點擊Do Nothing
(這什麼都不做),並再次旋轉你的鼠標滾輪,並且它正常打印!
我正在使用Windows 7 SP1 & JavaSE-1.6 64位。
這是我這有問題簡化代碼:
ControllerPane.java
import java.awt.*;
import javax.swing.*;
public class ControllerPane extends JPanel {
private static final long serialVersionUID = 1L;
public static final String PAGE_MAIN = "MAIN";
public static final String PAGE_LEVEL = "LEVEL";
private CardLayout layout;
public ControllerPane() {
setLayout(layout = new CardLayout());
add(new MainPane(), PAGE_MAIN);
add(new LevelPane(), PAGE_LEVEL);
}
public void setPage(String page) {
layout.show(this, page);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.add(new ControllerPane());
f.setSize(316, 338);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}
MainPane.java
import java.awt.event.*;
import javax.swing.*;
public class MainPane extends JPanel {
private static final long serialVersionUID = 1L;
public MainPane() {
setLayout(null);
JButton btnStartGame = new JButton("Go In");
btnStartGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
((ControllerPane) getParent())
.setPage(ControllerPane.PAGE_LEVEL);
}
});
btnStartGame.setBounds(50, 50, 200, 200);
add(btnStartGame);
}
}
LevelPane.java
import java.awt.event.*;
import javax.swing.*;
// FIXME mouseWheelMoved
public class LevelPane extends JPanel {
private static final long serialVersionUID = 1L;
public LevelPane() {
addMouseWheelListener(new DrawListener());
setLayout(null);
JButton btnRetry = new JButton("Do nothing");
btnRetry.setBounds(50, 50, 200, 200);
add(btnRetry);
}
private class DrawListener extends MouseAdapter {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
System.out.println(e);
}
}
}
工作正常,我... – MadProgrammer 2013-04-27 09:18:52
@MadProgrammer不工作的JavaSE-1.6 – johnchen902 2013-04-27 09:24:53
它並不實際,只是沒有你想要它,檢查我的詳細信息的回答方式;) – MadProgrammer 2013-04-27 09:47:29