Mouse Event and Key Event
Mouse events notify when the user uses the mouse (or similar input device) to
interact with a component. Mouse events occur when the cursor enters or exits a
component's onscreen area and when the user presses or releases one of the
mouse buttons.
Key events indicate when the user is typing at the keyboard. Specifically, key
events are fired by the component with the keyboard focus when the user presses
or releases keyboard keys
import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
public class MouseandKeyEvent extends JFrame
{
public MouseandKeyEvent()
{
initComponents();
}
public long time1,time2,time;
private void jButton1MouseClicked(MouseEvent evt) {
}
private void jButton1MouseEntered(MouseEvent evt)
{
jLabel1.setText("Hurray!! You have entered the Mouse Event Button");
jButton1.getParent().setBackground(Color.cyan);
}
private void jButton1MousePressed(MouseEvent evt)
{
time1 = System.currentTimeMillis();
jLabel1.setText("Mouse Button has been Pressed");
}
private void jButton1MouseReleased(MouseEvent evt)
{
time2 = System.currentTimeMillis();
time= (long)time2-time1;
jLabel1.setText("You had Presed the Button for " +time+ " mili-seconds");
}
private void jButton1MouseExited(MouseEvent evt)
{
jLabel1.setText("");
jButton1.getParent().setBackground(Color.WHITE);
}
private void jButton2KeyPressed(KeyEvent evt)
{
char key = evt.getKeyChar();
jLabel1.setText("You had Pressed the "+key+" key");
}
private void jButton2KeyTyped(KeyEvent evt) {
}
public static void main(String args[])
{
new MouseandKeyEvent().setVisible(true);
}
private JButton jButton1;
private JButton jButton2;
private JLabel jLabel1;
private JLabel jLabel2;
}
}
0 comments:
Post a Comment