这个程序假设创建一个窗口,它下面有一个状态栏,显示鼠标在屏幕上没有移动的情况下被点击了多少次。当您移动鼠标并单击它时,假设开始一个新的计数。它还区分不同的鼠标按钮。我完全按照我看到的教程遵循了这段代码,但它不起作用。我只是得到一个状态栏永远不会改变的窗口。
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);
}
}