Home > Enterprise >  Event Handling in java mouseListener problem
Event Handling in java mouseListener problem

Time:12-07

I only want the hover effect on my button which is "hover me" but I do not know why it is implemented on my full frame

CodePudding user response:

Your JButton takes up the entire inner area of your JFrame.

I modified your code to put the JButton in a JPanel, then put the JPanel in the JFrame.

Here's the revised GUI.

enter image description here

Here's the revised code.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class HoverEffect extends JFrame {

    private static final long serialVersionUID = 1L;

    JButton b;

    HoverEffect() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JPanel panel = new JPanel(new FlowLayout());
        b = new JButton("hover me");
        b.addMouseListener(new myMouse());
        panel.add(b);
        
        add(panel, BorderLayout.CENTER);
        
        setSize(200, 100);
        setVisible(true);
    }

    class myMouse implements MouseListener {
        @Override
        public void mouseClicked(java.awt.event.MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(java.awt.event.MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(java.awt.event.MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(java.awt.event.MouseEvent e) {
            b.setBackground(Color.GREEN);

        }

        @Override
        public void mouseExited(java.awt.event.MouseEvent e) {
            b.setBackground(Color.white);
        }
    }

    public static void main(String[] args) {
        new HoverEffect();
    }

}

CodePudding user response:

its my code

import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.MouseEvent.*;
// import javax.swing.event.MouseInputListener;

// import org.w3c.dom.events.MouseEvent;
public class hoverEffect extends JFrame{
JFrame f;  
JButton b;
hoverEffect(){
    b = new JButton("hover me");
    b.setBounds(100, 100, 30, 30);
    b.addMouseListener(new myMouse());
    add(b); 
    setVisible(true);
    setSize(200,300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

class myMouse implements MouseListener{
    @Override
    public void mouseClicked(java.awt.event.MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void mousePressed(java.awt.event.MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void mouseReleased(java.awt.event.MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void mouseEntered(java.awt.event.MouseEvent e) {
        b.setBackground(Color.GREEN);

        
    }
    @Override
    public void mouseExited(java.awt.event.MouseEvent e) {
        b.setBackground(Color.white);
    }
}
public static void main(String[] args) {
        hoverEffect h = new hoverEffect();
    }
}
  • Related