该程序假设创建一个窗口,该窗口下带有一个状态栏,该状态栏显示了在没有移动屏幕的情况下单击鼠标的次数。当您移动鼠标并单击它时,它应该开始新的计数。它还可以区分不同的鼠标按钮。我已经按照我所看到的教程完全遵循了此代码,但是它不起作用。我得到的窗口带有永远不会改变的状态栏。
public class Adapter_class extends JFrame {
private String details;
private JLabel statusBar;
public Adapter_class() {
super("Adapter mouse:");
this.statusBar = new JLabel("Default");
add(this.statusBar, BorderLayout.SOUTH);
addMouseListener(new MouseClass());
}
private class MouseClass extends MouseAdapter {
public void MouseClicked (MouseEvent event) {
details = String.format("You clicked the mouse %d", event.getClickCount());
//this is for using a mouse from a mac
if (event.isMetaDown())
details += " with the right mouse button";
else if (event.isAltDown())
details += " with the center mouse button";
else
details += " with the left mouse button";
statusBar.setText(details);
}
}
}
这是主要的:
import javax.swing.JFrame;
public class Adapter_main {
public static void main(String[] args) {
Adapter_class window = new Adapter_class();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 300);
window.setVisible(true);
}
}
最佳答案
public abstract class MouseAdapter implements MouseListener, MouseWheelListener, MouseMotionListener {
/**
* {@inheritDoc}
*/
public void mouseClicked(MouseEvent e) {}
用这个:
private class MouseClass extends MouseAdapter {
public void mouseClicked (MouseEvent event){
在这里,您必须编写的函数是
**mouseClicked**
而不是**MouseClicked**
这就是为什么使用注释会有所帮助的原因。
@Override会立即为您提供帮助。