Name : Telange Pranav
Deepak Roll no : 57
Batch : CO5I ‘A ’
Practical No. 10
Aim : Write a program to generate KeyEvent when a key is pressed and display
“Key Pressed” message.
Source Code:
import [Link].*;
import [Link].*;
import [Link].*;
public class KEvent extends JFrame implements KeyListener {
Label i;
TextArea t1;
KEvent() {
i = new Label();
t1 = new TextArea();
[Link](20, 50, 100, 20);
[Link](20, 80, 300, 300);
[Link](this);
add(i);
add(t1);
setSize(300, 300);
setVisible(true);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void keyPressed(KeyEvent ke) {
[Link]("Key Pressed");
}
public static void main(String[] args) {
new KEvent();
}
}
Output:
Aim : Develop a program which will implement special keys such as function
keys and arrow keys
Source Code:
import [Link].*;
import [Link].*;
public class KEvent implements KeyListener{
private Frame frame;
private Label l1;
public KEvent()
{
frame = new Frame("Key Press Listener");
l1 = new Label("Press a Key");
[Link](new Font("Arial",[Link],20));
[Link](l1);
[Link](300,200);
[Link](null);
[Link](true);;
[Link](this);
}
public void keyPressed(KeyEvent e)
{
int keycode = [Link]();
String keyName =
[Link](keycode); [Link]("Key
Pressed : "+keyName);
}
public void keyPressed(KeyEvent e)
{
int keycode = [Link]();
String keyName = [Link](keycode);
[Link]("Key Pressed : "+keyName);
}
public static void main(String[] args) {
new KEvent();
}
}
Output:
Aim : Develop a program to accept two numbers and display product of two
numbers when user pressed “Multiply” button.
Source Code:
import [Link].*;
import [Link].*;
import [Link].*;
public class Product extends JFrame implements ActionListener
{ private JTextField num1field, num2field, resultfield;
JButton mulbtn;
public Product() {
super("Product Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 2));
JLabel num1L = new JLabel("Number
1:"); num1field = new JTextField();
JLabel num2L = new JLabel("Number
2:"); num2field = new JTextField();
JLabel resultl = new JLabel("Result :");
resultfield = new JTextField();
[Link](false);
mulbtn = new JButton("Multiply");
[Link](this);
add(num1L);
add(num1field);
add(num2L);
add(num2field);
add(resultl);
add(resultfield);
add(new JLabel());
add(mulbtn);
setSize(300, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if ([Link]() == mulbtn) {
double num1 =
[Link]([Link]()); double num2
= [Link]([Link]()); double
result = num1 * num2; [Link](true);
[Link]([Link](result));
}
}
public static void main(String[] args) {
new Product();
}
}
Output:
Name : Telange Pranav
Deepak Roll no : 57
Batch : CO5I ‘A ’
Practical No. 11
Aim : Debug the following Program code and write the output.
Source Code:
import [Link];
import [Link].*;
import [Link].*;
/* <APPLET CODE="MouseDemo" WIDTH=300 HEIGHT=200> </APPLET> */
public class MouseDemo extends Applet implements MouseListener {
Label l;
public void init() {
setLayout(null);
l = new Label("Hello Mouse");
[Link](50, 70, 200, 100);
add(l);
addMouseListener(this);
}
public void mousePressed(MouseEvent e) {
[Link]("Mouse Pressed no. of clicks: " + [Link]() + " at position " + [Link]()
+ "," + [Link]());
}
public void mouseReleased(MouseEvent e) {
[Link]("Mouse Released; # of clicks: " + [Link]());
}
public void mouseEntered(MouseEvent e)
{ [Link]("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
[Link]("Mouse exited");
}
public void mouseClicked(MouseEvent e) {
[Link]("Mouse clicked (# of clicks: " + [Link]() + ")");
}
}
Output:
Aim : Write a program to change the background color of Applet when user
performs events using Mouse.
Source Code:
import [Link].*;
import [Link].*;
import [Link].*;
public class MouseEventA extends JFrame implements MouseListener {
public MouseEventA() {
setTitle("Mouse Event Background Color");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
getContentPane().setBackground([Link]);
}
public void mousePressed(MouseEvent e) {
getContent
public void mouseReleased(MouseEvent e) {
getContentPane().setBackground([Link]);
[Link]("Blue");
}
public void mouseEntered(MouseEvent e) {
getContentPane().setBackground([Link]);
}
public void mouseExited(MouseEvent e) {
getContentPane().setBackground(Color.LIGHT_GRAY);
}
public static void main(String[] args) {
[Link](MouseEventA::new);
}
}
Output:
Aim : Write a program to count the number of clicks performed by the user in a
Frame window.
Source Code:
import [Link].*;
import [Link].*;
public class ClickCounter extends JFrame implements MouseListener {
private int clickCount = 0;
private JLabel label;
public ClickCounter() {
setTitle("Click Counter");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel("Number of clicks: 0", [Link]);
[Link]([Link]().deriveFont(18.0f));
add(label);
addMouseListener(this);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
clickCount++;
[Link]("Number of clicks: " + clickCount);
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public static void main(String[] args) {
[Link](ClickCounter::new);
}
}
Output:
Aim : Write a program to demonstrate the use of mouseDragged and
mouseMoved method of MouseMotionListener.
Source Code:
import [Link].*;
import [Link].*;
import [Link].*;
public class MouseMotionDemo1 extends JFrame implements MouseMotionListener {
private int mouseX = 0;
private int mouseY = 0;
private String eventType =
"";
public MouseMotionDemo1() {
setTitle("Mouse Motion Demo");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent e) {
mouseX = [Link]();
mouseY = [Link]();
eventType = "Mouse Moved By
Pranav"; repaint();
}
public void mouseDragged(MouseEvent e) {
mouseX = [Link]();
mouseY = [Link]();
eventType = "Mouse
Dragged"; repaint();
}
public void paint(Graphics g) {
[Link](g);
[Link]([Link]);
[Link](eventType + " at (" + mouseX + ", " + mouseY + ")", 50, 50);
}
public static void main(String[] args) {
new MouseMotionDemo1();
}
}
Output: