我正在开发一个游戏,但我发现我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);
}
});
}
}
主窗格.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);
}
}
}