Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*; // Mouse events
- import java.util.logging.Handler;
- import javax.swing.*;
- public class Gui extends JFrame{
- // Variables
- private JPanel mousepanel;
- private JLabel statusbar;
- // Constructor
- public Gui(){
- super("JFrame's title");
- // setLayout(new BorderLayout());
- mousepanel = new JPanel();
- mousepanel.setBackground(Color.WHITE);
- add(mousepanel, BorderLayout.CENTER);
- statusbar = new JLabel("Default");
- add(statusbar, BorderLayout.SOUTH);
- // Implementing 2 classes
- Handlerclass handler = new Handlerclass();
- mousepanel.addMouseListener(handler);
- mousepanel.addMouseMotionListener(handler);
- }
- // New class
- private class Handlerclass implements MouseListener, MouseMotionListener{
- // MouseListener has 5 methods
- public void mouseClicked(MouseEvent event){
- statusbar.setText(String.format("Clicked at %d %d", event.getX(), event.getY()));
- }
- public void mousePressed(MouseEvent event){
- statusbar.setText(String.format("Pressed at %d %d", event.getX(), event.getY()));
- }
- public void mouseReleased(MouseEvent event){
- statusbar.setText(String.format("Released at %d %d", event.getX(), event.getY()));
- }
- public void mouseEntered(MouseEvent event){
- // When the mouse enters mousepanel
- statusbar.setText("You entered the area of events");
- mousepanel.setBackground(Color.GREEN);
- }
- public void mouseExited(MouseEvent event){
- // When the mouse enters mousepanel
- statusbar.setText("The mouse has left the window");
- mousepanel.setBackground(Color.RED);
- }
- // MouseMotionListener has 2 methods
- @Override
- public void mouseMoved(MouseEvent event) {
- // Without clicking mouse button
- statusbar.setText("You moved the mouse...");
- }
- @Override
- public void mouseDragged(MouseEvent event) {
- statusbar.setText("You are dragging your mouse!");
- }
- }
- // Main Function
- public static void main(String[] args){
- Gui gui = new Gui();
- gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
- gui.setSize(600, 600);
- gui.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement