this program suppose to create a window that has a status bar under it that shows how many times the mouse was clicked without being moved on the screen. when you move the mouse and click it suppose to start a new count. it also distinguishes different mouse buttons. I’ve followed this code exactly as a tutorial I saw, but it doesn’t work. I just get the window with a status bar that never changes.
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);
}
}
}
this is the main:
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);
}
}
Use this:
Here the function you have to write is
**mouseClicked**not**MouseClicked**Thats why it helps to use annotations.
@Override would have helped you immediately.