【问题标题】:How to put Hover effect on jbutton?如何在jbutton上放置悬停效果?
【发布时间】:2014-03-25 15:20:41
【问题描述】:

我正在尝试创建一个使用两个按钮的 Java 桌面应用程序。我想在这些按钮中添加悬停效果。我想要:当我点击任何按钮时,它应该改变它的背景颜色。

我怎样才能实现它?

这是我的代码:

public class Party1Party2 extends JFrame
{
    JButton b1;
    JButton b2;
    Container pane;

    public Party1Party2()
    {
        getContentPane().setBackground(new java.awt.Color(255, 255, 255));

    b2.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            JOptionPane.showMessageDialog(frame, "Welcome to allhabad High Court");
        }
    });

    b1.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            JOptionPane.showMessageDialog(frame, "Welcome to allhabad High Court");

        }
    });
  }
}

【问题讨论】:

  • @peeskillet 可以告诉我如何将 jbutton 制作为圆角矩形
  • 老实说,我只是考虑改变外观。我不喜欢弄乱组件的外观。见this postModifying the Look and Feel
  • 所有关于鼠标监听器的尝试都是错误的,有副作用,忽略这些答案
  • @mKorbel -- 你能否请我指出更多关于为什么(或何时)使用鼠标监听器不好的知识,以及首选的替代方法是什么? (不一定在 OP 的问题中)谢谢。十五分钟的谷歌搜索对我没有任何帮助。

标签: java swing jbutton mouselistener rollover


【解决方案1】:

您可以使用鼠标EnteredExitedJButton,做任何你想做的事。

例如:

jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseEntered(java.awt.event.MouseEvent evt) {
        jButton1.setBackground(Color.GREEN);
    }

    public void mouseExited(java.awt.event.MouseEvent evt) {
        jButton1.setBackground(UIManager.getColor("control"));
    }
});

【讨论】:

  • 答案错了,所有鼠标事件都是在JButtons API中实现的,并正确使用它们而不是鼠标监听器
  • 哦 eeeeasy,我想你是想说有更好的方法来做到这一点,但这不是一个错误的答案,至少对我来说,因为我做了很多次,请在你的 cmets 中更现代!!!
  • 好的,那么请在下次您要编辑我的答案或通知我有更好的方法使用其他方法时明确表示,这在您之前的评论中并不清楚你说ignore those answersanswer is wrong,谢谢你的提示,我会更新我的答案。
  • 这个讨论如果没有,特别是因为没有提到“更好”的想法。我在这里找到它:stackoverflow.com/questions/16733708/…
  • 什么是“控制”?
【解决方案2】:

我曾经写过一个自定义的 JButton,当鼠标悬停在它上面时,它会通过动画改变它的透明度。这是该按钮的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class HoverButton extends JButton
{
  float alpha = 0.5f;

  public HoverButton(String text)
  {
    super(text);
    setFocusPainted(false);
    setBorderPainted(false);
    setContentAreaFilled(false);
    addMouseListener(new ML());
  }

  public float getAlpha()
  {
    return alpha;
  }

  public void setAlpha(float alpha)
  {
    this.alpha = alpha;
    repaint();
  }

  public void paintComponent(java.awt.Graphics g)
  {
    java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    super.paintComponent(g2);
  }

  public class ML extends MouseAdapter
  {
    public void mouseExited(MouseEvent me)
    {
      new Thread(new Runnable()
      {
        public void run()
        {
          for (float i = 1f; i >= .5f; i -= .03f)
          {
            setAlpha(i);
            try
            {
              Thread.sleep(10);
            }
            catch (Exception e)
            {
            }
          }
        }
      }).start();
    }

    public void mouseEntered(MouseEvent me)
    {
      new Thread(new Runnable()
      {
        public void run()
        {
          for (float i = .5f; i <= 1f; i += .03f)
          {
            setAlpha(i);
            try
            {
              Thread.sleep(10);
            }
            catch (Exception e)
            {
            }
          }
        }
      }).start();
    }

    public void mousePressed(MouseEvent me)
    {
      new Thread(new Runnable()
      {
        public void run()
        {
          for (float i = 1f; i >= 0.6f; i -= .1f)
          {
            setAlpha(i);
            try
            {
              Thread.sleep(1);
            }
            catch (Exception e)
            {
            }
          }
        }
      }).start();
    }
  }
}

下面是HoverButton的快速演示:

import javax.swing.*;
import java.awt.*;

public class Demonstration
{
  public Demonstration()
  {
    JFrame frame = new JFrame("Hover Button Demonstration");
    frame.setLayout(new GridBagLayout());
    frame.add(new HoverButton("Hover Button!!"));

    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }

  public static void main(String args[])
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      @Override
      public void run()
      {
        new Demonstration();
      }
    });
  }
}

好消息是您可以调整代码以更改按钮的背景颜色,而且还可以以动画方式。

【讨论】:

    【解决方案3】:

    哇。老问题,我知道,但是...

    要更改背景,请使用:

    b1.setBackground(new java.awt.Color(r, g, b));
    

    在 actionListener 中。

    对于翻转效果,您可以使用:

    b1.setRolloverEnabled(true);
    

    但您需要为按钮提供图标以便在它们之间切换。

    否则,对于其他悬停效果,您确实需要使用 mouseListener。

    【讨论】:

      【解决方案4】:

      @Salah 的回答起初对我不起作用,但在我使用禁用按钮的翻转效果后它确实起作用了

      button1.setRolloverEnabled(false);
      

      翻转效果自动设置了我按钮的悬停颜色,它覆盖了我在mouseEntered() 中设置的颜色。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-23
        • 2013-07-02
        • 2016-06-15
        • 1970-01-01
        • 1970-01-01
        • 2013-05-06
        • 1970-01-01
        • 2021-05-21
        相关资源
        最近更新 更多