Portfolio
MouseEvents
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gui extends JFrame {
private JPanel mousepanel;
private JLabel statusbar;
public Gui() {
super("title");
mousepanel = new JPanel();
mousepanel.setBackground(Color.WHITE);
add(mousepanel, BorderLayout.CENTER);
statusbar = new JLabel("defa");
add(statusbar, BorderLayout.SOUTH);
Handlerclass handler = new Handlerclass();
mousepanel.addMouseListener(handler);
mousepanel.addMouseMotionListener(handler);
}
private class Handlerclass implements MouseListener, MouseMotionListener {
public void mouseClicked(MouseEvent event) {
statusbar.setText(String.format("Clicked at %d, %d", event.getX(), event.getY()));
}
public void mousePressed(MouseEvent event) {
statusbar.setText("You pressed down the mouse");
}
public void mouseReleased(MouseEvent event) {
statusbar.setText("You released the mouse");
}
public void mouseEntered(MouseEvent event) {
statusbar.setText("You entered the area");
mousepanel.setBackground(Color.RED);
}
public void mouseExited(MouseEvent event) {
statusbar.setText("the mouse has left the area");
mousepanel.setBackground(Color.WHITE);
}
//these are mouse motion events
public void mouseDragged(MouseEvent event) {
statusbar.setText("You are dragging the mouse");
}
public void mouseMoved (MouseEvent event) {
statusbar.setText("you moved the mouse");
}
}
}
import javax.swing.JFrame;
public class aplles {
public static void main(String[] args) {
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(640,480);
go.setVisible(true);
}
