Practical 11 java

 avatar
Rohit143
java
2 years ago
2.0 kB
3
Indexable
Never
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.MOVE_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){
        setCursor(new Cursor(Cursor.MOVE_CURSOR));
        position.setText("Mouse drag position -> X: "+e.getX()+" Y: "+e.getY());
        

        


    }
    public void mouseMoved(MouseEvent e){
        validate();
        position.setText(" MOuse move position -> X: "+e.getX()+" Y: "+e.getY());

    }

}

public class practical11 {
    public static void main(String[] args) {
        new MyFrame();
    }
    
}