2

The frame opens and close normally but mouse click doesn't work.

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Create a frame window that responds to mouse click
public class AWT3 extends Frame {
 String Mmsg="";
 int mouseX=0, mouseY=0;
 public AWT3() {
 addWindowListener(new MyWindowwAdapter(this));
 addMouseListener(new MyMouseeAdapter(this));
 }
public void paint(Graphics g){
 g.drawString(Mmsg, mouseX, mouseY);
 }
public static void main(String args[]){
 AWT3 awt3 = new AWT3();
 awt3.setSize(new dimension(500, 500));
 awt3.setTitle("Window framee");
 awt3.setVisible(true);
 }
}
class MyWindowwAdapter extends WindowAdapter{
 AWT3 awt3;
 public MyWindowwAdapter(AWT3 awt3) {
 this.awt3=awt3;
 }
 public void windowClosing(WindowEvent we){
 awt3.setVisible(false);
 }
}
class MyMouseeAdapter extends MouseAdapter{
AWT3 awt3;
public MyMouseeAdapter(AWT3 awt3) {
 this.awt3=awt3;
}
public void MouseClicked(MouseEvent me){
 awt3.Mmsg="the mouse is clicked";
 awt3.mouseX= me.getX();
 awt3.mouseY=me.getY();``
 awt3.repaint();
}
}
Andrew Thompson
169k42 gold badges224 silver badges441 bronze badges
asked May 25, 2015 at 10:57
3
  • and awt3.setSize(new Dimension(500, 500)); Commented May 25, 2015 at 11:03
  • also has to import the class. import java.awt.Dimension; Commented May 25, 2015 at 11:07
  • Why use AWT? See this answer for many good reasons to abandon AWT using components in favor of Swing. Commented May 26, 2015 at 11:42

3 Answers 3

2

From what it looks like, this code won't compile. You have an error that you need to fix:

awt3.setSize(new dimension(500, 500));

to

awt3.setSize(new Dimension(500, 500));

and add the proper import java.awt.Dimension as pointed out by others.

Another mistake is that MouseClicked(MouseEvent me) is not overriding the super class method from MouseAdapter as its syntactically wrong (super class method starts with small case). Change it to mouseClicked(MouseEvent me) (add the optional @Override annotation if you wish).

answered May 25, 2015 at 12:09
Sign up to request clarification or add additional context in comments.

1 Comment

"add the optional @Override annotation .." +1.
0

The method name should be public void mouseClicked(MouseEvent me) instead of public void MouseClicked(MouseEvent me).

answered May 25, 2015 at 11:05

Comments

0

mouseClicked() is when the mouse button has been pressed and released.

mousePressed() is when the mouse button has been pressed.

Your code is working. tested on java 1.7. only the problem I saw, was with out importing the java.awt.Dimension class you are trying to create a new dimension(500, 500); although the class name is in simple form you can fix this error and try the code.

answered May 25, 2015 at 11:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.