0% found this document useful (0 votes)
3 views

java Practical 20

The document contains two Java Swing applications: KeyPressDemo and MouseMotionDemo. KeyPressDemo captures key press events and displays the pressed key, while MouseMotionDemo tracks mouse movements and drags, updating the display with the mouse coordinates. Both applications utilize JFrame and event listeners to provide interactive user experiences.

Uploaded by

londheprerana72
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

java Practical 20

The document contains two Java Swing applications: KeyPressDemo and MouseMotionDemo. KeyPressDemo captures key press events and displays the pressed key, while MouseMotionDemo tracks mouse movements and drags, updating the display with the mouse coordinates. Both applications utilize JFrame and event listeners to provide interactive user experiences.

Uploaded by

londheprerana72
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical 20:Q3

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyPressDemo extends JFrame implements KeyListener
{
JLabel label;
public KeyPressDemo()
{
setTitle("Key Press Event");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
label = new JLabel("Press any key...");
label.setFont(new Font("Arial", Font.BOLD, 20));
add(label);
addKeyListener(this);
setFocusable(true);
setVisible(true);
}
public void keyPressed(KeyEvent e)
{
label.setText("Key Pressed: " + e.getKeyChar());
}
public static void main(String[] args)
{
new KeyPressDemo();
}
}

Q 4:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class MouseMotionDemo extends JFrame implements MouseMotionListener
{
JLabel label;
public MouseMotionDemo()
{
setTitle("Mouse Motion Listener");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
label = new JLabel("Move or Drag the Mouse...");
label.setFont(new Font("Arial", Font.BOLD, 16));
add(label);
addMouseMotionListener(this);
setVisible(true);
}
public void mouseMoved(MouseEvent e)
{
label.setText("Mouse Moved to: X=" + e.getX() + ", Y=" + e.getY());
}
public void mouseDragged(MouseEvent e)
{
label.setText("Mouse Dragged to: X=" + e.getX() + ", Y=" + e.getY());
}
public static void main(String[] args)
{
new MouseMotionDemo();
}
}

You might also like