Practical 11

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Advanced Java Programming

Name :Krishna Raut CO5I Roll No. : 25

Q. Write a program to demonstrate various mouse events


CODE:
import javax.swing.*;
import java.awt.event.*;

public class practical11A extends JFrame implements MouseListener


{ private JLabel label;

public practical11A()
{ setTitle("Mouse Demo");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
label = new JLabel("Hello Mouse");
label.setBounds(50, 150, 200,
100); add(label);
addMouseListener(this);`
}

public void mousePressed(MouseEvent e) {


label.setText("Mouse Pressed; # of clicks: " + e.getClickCount() +
" at position " + e.getX() + ", " + e.getY());
}

public void mouseReleased(MouseEvent e) {


label.setText("Mouse Released; # of clicks: " + e.getClickCount());
}
Advanced Java Programming

public void mouseEntered(MouseEvent e)


{ label.setText("Mouse Entered");

public void mouseExited(MouseEvent e)


{ label.setText("Mouse Exited");
}

public void mouseClicked(MouseEvent e) {


label.setText("Mouse Clicked; # of clicks: " + e.getClickCount());
}

public static void main(String[] args)


{ practical11A frame = new
practical11A(); frame.setVisible(true);
}
}
OUTPUT:
Advanced Java Programming

Q.Program to count the number of clicks performed by the user in a frame


window.
CODE:
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class practical11B extends Frame implements MouseListener


{ int flag = 0;
Label l;
practical11B() {
l = new Label();
add(l);
addMouseListener(this);
setVisible(true);
setSize(400, 400);
setLayout(new FlowLayout());
}

public void mousePressed(MouseEvent e) {


}

public void mouseReleased(MouseEvent e) {


}

public void mouseEntered(MouseEvent e) {


}
Advanced Java Programming

public void mouseExited(MouseEvent e) {


}

public void mouseClicked(MouseEvent e) {


setBackground(Color.ORANGE);
flag++;
l.setText("NO. OF CLICKS:) = " + flag);
}

public static void main(String[] args) {


new practical11B();
}
}
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy