Practical 20:Q3
import [Link].*;
import [Link].*;
import [Link];
import [Link];
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...");
[Link](new Font("Arial", [Link], 20));
add(label);
addKeyListener(this);
setFocusable(true);
setVisible(true);
}
public void keyPressed(KeyEvent e)
{
[Link]("Key Pressed: " + [Link]());
}
public static void main(String[] args)
{
new KeyPressDemo();
}
}
Q 4:
import [Link].*;
import [Link].*;
import [Link];
import [Link];
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...");
[Link](new Font("Arial", [Link], 16));
add(label);
addMouseMotionListener(this);
setVisible(true);
}
public void mouseMoved(MouseEvent e)
{
[Link]("Mouse Moved to: X=" + [Link]() + ", Y=" + [Link]());
}
public void mouseDragged(MouseEvent e)
{
[Link]("Mouse Dragged to: X=" + [Link]() + ", Y=" + [Link]());
}
public static void main(String[] args)
{
new MouseMotionDemo();
}
}