practical 9 updated
Rohit143
java
4 years ago
2.2 kB
27
Indexable
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyFrame extends JFrame implements MouseListener,MouseMotionListener{
    JButton button;
    JLabel label;
    JLabel position;
    MyFrame(){
        button =new JButton("Button");
        label= new JLabel("Play with button");
        position= new JLabel("position");
        button.setBounds(50, 50, 100, 40);
        
        add(button);
        add(label);
        add(position,BorderLayout.SOUTH);
        button.addMouseListener(this);
        addMouseMotionListener(this);
        setDefaultCloseOperation(3);
        setVisible(true);
        setSize(400, 500);
    }
    // mouseListener methods
    public void mousePressed(MouseEvent e){
        validate();
        button.setBackground(Color.PINK);
        label.setText("Mouse is Pressed");
    }
    public void mouseReleased(MouseEvent e){
        validate();
        label.setText("mouse is released");
    }
    public void mouseEntered(MouseEvent e){
        validate();
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        button.setBackground(Color.orange);
        label.setText("mouse is Entered");
    }
    public void mouseExited(MouseEvent e) {
        validate();
        button.setBackground(Color.cyan);
        label.setText("mouse is Exited");
    }
    public void mouseClicked(MouseEvent e){
        validate();
        label.setText("mouse is Clicked");
    }
    // mouseMotionListener method
    public void mouseDragged(MouseEvent e){
        validate();
        setCursor(new Cursor(Cursor.MOVE_CURSOR));
        label.setText("mouse is Exited and mouse is dragging");
        position.setText("Mouse drag position -> X: "+e.getX()+" Y: "+e.getY());
    }
    public void mouseMoved(MouseEvent e){
        validate();
        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        label.setText("mouse is Exited and mouse is moving");
        position.setText(" Mouse move position -> X: "+e.getX()+" Y: "+e.getY());
    }
}
public class practical11 {
    public static void main(String[] args) {
        new MyFrame();
    }
    
}
Editor is loading...