VB_Lecture_4
VB_Lecture_4
3. Override the
WindowListener methods
WindowListener - Example
FrameDemo.Java
WindowListener - Example
NewClass.Java
Swing – Frame Containment
Hierarchy
WindowListener - Questions
Question: write a java code to change the background color of the
window to red when the window is changed from a minimized to a
normal state. Use the below template code.
WindowListener - Questions
Solution
Event Driven Programming
Action Event
Window Event
Mouse Event
Key Event
Focus Event
Item Event
Swing – MouseEvent Class
Mouse event occurs when a mouse related activity is
performed on a component such as clicking, dragging,
pressing, moving or releasing a mouse etc.
3.Override the
MouseListener methods
MouseListener Example
FrameDemo.Java
MouseListener Example
NewClass.Java
MouseListener - Questions
Question: write a java code to move NewLabel to the position of the
mouse when it is clicked. Use the below template code.
MouseListener - Questions
Answer
Methods of
MouseMotionListener interface
Method Description
mouseDragged(MouseEvent) Called in response to the user moving the
mouse while holding a mouse button down.
This event is fired by the component that
fired the most recent mouse-pressed event,
even if the cursor is no longer over that
component.
mouseMoved(MouseEvent) Called in response to the user moving the
mouse with no mouse buttons pressed.
This event is fired by the component that's
currently under the cursor.
How to write
MouseMotionListener?
If you implement the MouseMotionListener class,
you need to follow 3 steps:
1. Implement the MouseMotionListener interface
in the class
2. Register the component with the
MouseMotionListener
3. Override the
MouseMotionListener methods
MouseMotionListener -
Example
MouseMotionListener -
Example
2D Graphics - Swing
Method Description
public abstract void is used to draw the specified string.
drawString(String str, int x, int y)
public void drawRect(int x, int y, int draws a rectangle with the specified
width, int height) width and height
public abstract void fillRect(int x, int is used to fill rectangle with the default
y, int width, int height) color and specified width and height.
public abstract void drawOval(int x, is used to draw oval with the specified
int y, int width, int height) width and height.
public abstract void fillOval(int x, int is used to fill oval with the default color
y, int width, int height) and specified width and height.
public abstract void drawLine(int is used to draw line between the
x1, int y1, int x2, int y2) points(x1, y1) and (x2, y2).
2D Graphics - Swing
Method Description
public abstract boolean drawImage(Image is used draw the specified
img, int x, int y, ImageObserver observer) image.
public abstract void drawArc(int x, int y, int is used draw a circular or
width, int height, int startAngle, int arcAngle) elliptical arc.
public abstract void fillArc(int x, int y, int width, is used to fill a circular or
int height, int startAngle, int arcAngle) elliptical arc.
public abstract void setColor(Color c) is used to set the graphics
current color to the specified
color.
public abstract void setFont(Font font) is used to set the graphics
current font to the specified
font.
2D Graphics - Example
Java Swing: Notes
Ways to create a frame:
1. By creating the object of Frame class
(association)
import javax.swing.*;
public class Swing_example
{
public static void main(String[] args)
{
JFrame frame1 = new JFrame();
JButton button1 = new JButton("click");
JButton button2 = new JButton("again click");
button1.setBounds(160, 150 ,80, 80);
button2.setBounds(190, 190, 100, 200);
frame1.add(button1);
frame1.add(button2);
frame1.setSize(400, 500) ;
frame1.setLayout(null);
frame1.setVisible(true);
}
}
64