Mouse over events with JButton

前端 未结 2 418
迷失自我
迷失自我 2021-01-14 10:02

I\'m trying to create a custom mouse over event on a JButton. The reason being that my JButton is currently an image, so I had to remove all the borders and animations and w

2条回答
  •  庸人自扰
    2021-01-14 10:41

    As an alternative You can achieve this by registering MouseListener to the JButton and override mouseEntered() ,mouseExited() , mousePressed() and mouseReleased() method.For Example:

            final ImageIcon icon1 = new ImageIcon("tray.gif");
            final JButton button = new JButton(icon1);
            final int width = icon1.getIconWidth();
            final int height = icon1.getIconHeight();
            button.addMouseListener(new MouseAdapter()
            {
                public void mouseEntered(MouseEvent evt)
                {
                    icon1.setImage((icon1.getImage().getScaledInstance(width + 10, height,Image.SCALE_SMOOTH)));
                    //button.setIcon(icon1);
                }
                public void mouseExited(MouseEvent evt)
                {
                    icon1.setImage((icon1.getImage().getScaledInstance(width , height,Image.SCALE_SMOOTH)));
                }
                public void mousePressed(MouseEvent evt)
                {
                    icon1.setImage((icon1.getImage().getScaledInstance(width + 5, height,Image.SCALE_SMOOTH)));
                }
                public void mouseReleased(MouseEvent evt)
                {
                    icon1.setImage((icon1.getImage().getScaledInstance(width + 10, height,Image.SCALE_SMOOTH)));
                }
            });
            button.setOpaque(false);
            button.setContentAreaFilled(false);
            button.setBorderPainted(false);
    

提交回复
热议问题