Just for learning purpose I am making a gui in which wherever a user click on window a text " mouse clicked" will appear at current position of mouse pointer.
I have used listener interfaces.
Expand|Select|Wrap|Line Numbers
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.*;
- public class MouseEvents extends Frame implements MouseListener,WindowListener{
- String ss = "mouse clicked";
- int mousex,mousey;
- public void windowClosed(WindowEvent e) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- public void windowDeiconified(WindowEvent e) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- public void windowIconified(WindowEvent e) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- public void windowOpened(WindowEvent e) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- public void windowDeactivated(WindowEvent e) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- public void windowActivated(WindowEvent e) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- public void mouseExited(MouseEvent e) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- public void mousePressed(MouseEvent e) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- public void mouseClicked(MouseEvent e) {
- mousex = e.getX();
- mousey = e.getY();
- repaint();
- }
- @override
- public void paint(Graphics g){
- g.drawString(ss, mousex, mousey);
- }
- public void mouseEntered(MouseEvent e) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- public void mouseReleased(MouseEvent e) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- public void MouseEvents(){
- addMouseListener(this);
- addWindowListener(this);
- }
- public static void main(String args[]){
- MouseEvents ee = new MouseEvents();
- ee.setSize(500, 500);
- ee.setVisible(true);
- }
- }
But this code is not working according to expectations. I am not getting "mouse clicked" text and window doesn't get closed. I think my listener is not getting registered in my program. Also I have to use @Override at line 52 other wise netbeans is showing message here "multiple annotation here[2] - click to cycle."