Cs1102 Unit 6 Discussion Post
Cs1102 Unit 6 Discussion Post
and then connect things together in a summation at the bottom. This, I suppose, will be a wonderful way
to approach the subject this week.
Import Statement
import java.awt.event.*;
import javax.swing.*;
The import statements make things easier by supplying the necessary class/interface bundles to be
imported. Please refer to the class specified in this file for more information.
Java.awa.event.* This is used to obtain entry to the MouseListner interface, as well as related inherited
methods and property access.
javax.swing.* It's being used to gain access to JFrame and other inheriting methods, as well as to JFrame
and other inherited variables.
Class definition
This class extends the JFrame class and supports the MouseListener interface. MouseWhisperer formed
two "IS-A" relationships as a result of this action. The MouseWhisperer class is both a JFrame and a
Mouse Listener, according to its specification. This knowledge will aid comprehension of the remainder
of the code. Because JFrame is one of the top-level containers supported in the framework, extending it
makes the class a best container (Hock-Chuan, 2018).
Constructor definition
Super("COME CLOSER") – When we run the program, we can see that the title of the frame is set to
"COME CLOSER" in the class constructor. The super("COME CLOSER"); call is used to do this. This
essentially acts as a call to action.
the superclass constructor, which takes a string parameter and is described in the class
javax.swing.JFrame. The Frame title was set to the value we gave into this constructor.
Call to the constructor. As a result, when we create a JFrame object of the class MouseWhisperer.
The title will be changed to the value specified. "Creates a new, initially invisible Frame with the supplied
title," according to Oracle's definition of the constructor (Constructor Summary, n.d.).
setSize(300,100);
The JFrame class from javax.swing SetSize is called on the current object ("this") when the constructor is
called, and this is used to set the top-level window's dimensions (JFrame we created when we initialize
the object of the MouseWhisperer class). (Oracle,n.d.).
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
When we generated the upper edge frame (the window that opens with a close button on the top right),
the close button's functionality was set to conceal the frame by default. We override this capability by
providing EXIR ON CLOSE, which tells the program to use system exit functionality instead.
((System.exit()) (Oracle, n.d.)
addMouseListener(this);
The java.awt.Component class has a method that inherits from it. This method takes a MouseListener
object as an argument. We do pass 'this' into the method, as you can see in the code. Passing the
existing piece is correct because the object we defined inherits from the MouseListener interface, and
this enables us to connect the mouse lister to the object of interest (which is the JFrame) to receive
mouse events. This code can be rewritten as addMouseListner (this). The first this corresponds to the
current object's JFrame, whereas the second this refers to the current object's MouserListener. So,
basically, we connect a listener to the JFrame, and when something happens, the listener is triggered.
The addMouseListner method takes the current object and knows about the overridden methods in our
class (mouseClicked/mousePressed/etc).
setVisible(true);
Until recently, the JFrame has been hidden by default, despite the fact that we built it. To make it visible
on the screen, use the setVisible methods. This function is also a descendant of the java.awt.Component
class. (Oracle, n.d.).
public void mouseEntered(MouseEvent e) { setTitle("I SEE YOU"); } // When the mouse enters the
source, this event is triggered.
These are the abstract methods that were passed down through the interface implementation.
And they're all overridden to execute a certain duty, in this case using the setTitle method. Because
these are instance methods and the class is a JFrame (because the MouseWhisperer class extends from
JFrame), when these methods are called, they will be on a JFrame kind of object, which means they will
set the JFrame's title. Each method takes a MouseEvent parameter, which contains information about
the type of event and its properties.
The above code simply creates a new instance of the MouseWhiserer class, which in turn causes the
JFrame to be created.
Let me explain the behavior of the user interface as a whole now that we've delved deep into what
each line of code performs.
We'll begin with the main method, which is where the invocation begins. When the program is run, the
main methods will create an instance of the MouseWhisperer class, which will then call the class
constructor, which contains the majority of the code. We include an in the constructor.
event listener, which is a mouse event listener for the JFrame object we just created.
We changed the title of the first window (top-frame) to "COME CLOSER," increased the frame's size to
300 by 100 pixels, and made it visible on the screen.
When we move the mouse pointer into the frame, this interaction generates a mouse event (an event
object that encapsulates the status of an event, in this case, a mouse entered event), which is
transmitted to all registered listeners. The listener is defined in our code as follows:
Object JFrame
Following that, the listener will call a function to deal with the triggered event. The instance function
"public voidmouseEntered(MouseEvent e)" will be invoked in our example, and the top-level frame title
will be displayed.
"I SEE YOU" will be placed as the value. It's worth noting that we never said that in our code. The system
is in charge of this. Based on the interface it implements, the type of it, and the methods defined in the
interface, the system understands which method to call in response to a specific event. "Call back
methods" are methods that are invoked in this way (Hock-Chuan, 2018).
When a user clicks on the JFrame, it generates a mouseclick event, which is received by all registered
listeners and handled by a designated method, as mentioned in the preceding paragraph. The
mouseClicked method will set the JFrame (main window) title to "OUCH" in our example, as specified in
the method implementation. This behavior also applies to the mousePressed, mouseReleased, and
mouseExited events defined in our class.
When an event is triggered, the source object will notify the event to all the registered events (this is
known as subscribes) (which is known as publishing). As a result, the Subscribe-publish design pattern is
named after this approach (Hock-Chuan, 2018).
References
Hock-Chuan, C. (2018, April). Java programming tutorial: Programming graphical user interface
(GUI). http://www3.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html
Oracle (n.d.), Java™ Platform, Standard Edition 7 API Specification, Retrieved March 07, 2021
from https://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html
Oracle (n.d.), Java™ Platform, Standard Edition 7 API Specification, Retrieved March 07, 2021
from https://docs.oracle.com/javase/7/docs/api/java/awt/Window.html
Oracle (n.d.), Java™ Platform, Standard Edition 7 API Specification, Retrieved March 07, 2021
from https://docs.oracle.com/javase/7/docs/api/java/awt/Component.html
Oracle (n.d.), Java™ Platform, Standard Edition 7 API Specification, Retrieved March 07, 2021
from https://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseListener.html