It might also be interesting to explain its derived adapter class.
The MouseListener interface has an abstract class implementation called MouseAdapter.
The advantage of using the adapter class instead of the interface is that when you only need one method (say, mouseClicked, as in the final example), you don't need to implement the other ones.
The code, when using MouseAdapter, would then look like this:
Java5 Code:
package tutorials;
public MouseMethods() {
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
label.addMouseListener(this);
button.addMouseListener(this);
frame.add(label);
frame.add(button);
frame.setVisible(true);
frame.pack();
}
if(e.getSource().equals(button)){
System.
out.
println("The JButton was clicked...");
} else if(e.getSource().equals(label)){
System.
out.
println("The JLabel was clicked...");
} else {
System.
out.
println("Something else was clicked...");
}
}
public static void main
(String args[]
) { new MouseMethods();
}
}