Sunday, January 12, 2014

Java Swing Example Program 3

//Java Swing Example Program 3

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

public class MouseActions implements MouseListener,MouseMotionListener {
//declare variable
private JFrame f;
private JTextField tf;

public MouseActions(){
f = new JFrame("MouseFrame");
tf = new JTextField(15);
}

public void dispFrame(){
f.setLayout(new FlowLayout(FlowLayout.CENTER));
f.addMouseListener(this);
f.addMouseMotionListener(this);
f.add(tf);
f.setVisible(true);
}
public void mousePressed(MouseEvent e){
tf.setText("MousePressed");
try{
Thread.sleep(500);
}catch(Exception eq){}
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){tf.setText("Mouse Clicked");}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseDragged(MouseEvent e){
tf.setText("Mouse Dragged");
}
public void mouseMoved(MouseEvent e){}

public static void main(String arg[]){
MouseActions mevent = new MouseActions();
mevent.dispFrame();
}
}


No comments: