AWT Frame does not handle events
NickName:beck Ask DateTime:2015-05-25T18:57:10

AWT Frame does not handle events

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();
}
}

Copyright Notice:Content Author:「beck」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30436736/awt-frame-does-not-handle-events

Answers
ares 2015-05-25T12:09:42

From what it looks like, this code won't compile. You have an error that you need to fix:\n\nawt3.setSize(new dimension(500, 500));\n\n\nto\n\nawt3.setSize(new Dimension(500, 500));\n\n\nand add the proper import java.awt.Dimension as pointed out by others.\n\nAnother 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).",


shruti1810 2015-05-25T11:05:05

The method name should be public void mouseClicked(MouseEvent me)\ninstead of public void MouseClicked(MouseEvent me).",


More about “AWT Frame does not handle events” related questions

AWT Frame does not handle events

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....

Show Detail

Why are events not posted to AWT EventQueue?

I have the following piece of code import java.awt.*; import java.awt.event.*; import java.lang.reflect.*; import javax.swing.*; class QueueTest { public static void main(String[] args) throws

Show Detail

How to handle close button in AWT Frame

I am starting with some basic Java2D examples -specifically under AWT-, following first examples of book "Introduction to Computer Graphics Java2D/Java3D" I have written the following two classes, ...

Show Detail

Is there a way to intercept AWT events at the head end of the system Queue?

I have a graphics app and would like to (read only) be able to read events on the main event queue when they are deposited in the queue so they are not blocked by rendering. Is there a way to do t...

Show Detail

Clojure (let [frame (java.awt.Frame.)]) within un-invoked function causes AWT to be started

Whilst going through the Joy of Clojure book, I've succeedding in defining a function that, when invoked, will create and draw on a java.awt.Frame. (defn draw-frame [f x y] (let [frame (java.awt.

Show Detail

Java AWT Draw on Translucent Frame

I am having trouble drawing on a translucent frame. When the "alphaValue" is 255 everything works as expected. But I need a translucent frame. I created a small test class below that demonstrate...

Show Detail

How to handle events from keyboard and mouse in full screen exclusive mode in java?

In passive rendering mode one can use KeyListener and ActionListener interfaces to handle events from user. What is the correct way of event handling in full screen mode? Please extend this skele...

Show Detail

Resizable scrollpane to an awt frame

I need to add a scrollable JPanel to a AWT frame that can scale when the frame is re-sized. The scrollpane appears when I set it a fixed size. But I need the panel to cover the whole frame and re-s...

Show Detail

JRuby Closing an AWT frame

I decided to check out JRuby and JOGL to see if I could get some graphics sim stuff running. I've got the classpath set up and the OpenGL things included properly. Following some tutorials, they su...

Show Detail

How do you make key bindings for a java.awt.Frame?

Background My window is a java.awt.Frame, and inside of the Frame are two Panels (java.awt.Panel). I'm trying to make it so that the window handles buttons I press. Try Number 1 I tried using a

Show Detail