java - 如何在 jbutton 上设置悬停效果?

标签 java swing jbutton mouselistener rollover

我正在尝试创建一个使用两个按钮的 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");

        }
    });
  }
}

最佳答案

您可以使用鼠标EnteredExited JButton,做任何您想做的事。

例如:

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"));
    }
});

关于java - 如何在 jbutton 上设置悬停效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22638926/

相关文章:

Java无法发送https请求 "unable to find valid certification path"

java - 删除并添加回 JPanel 后,MouseEvent 不会在 JList 中触发

隐藏父窗口后拍摄的 Java 屏幕截图

java - 检测特殊字符的按键

java - 用java制作一个计算器,如何只允许数字输入

java - 功能测试 (Jellytools) 不在 netbeans 平台上启动

java - 应用范围内的 Seam 组件的许多实例

java - 在 Java 中执行命令而不重定向输出

java - 使用 MouseListener/MouseMotionListener 移动对话框时如何减少 Java Swing 抖动/闪烁/摇晃?

java - 我想更改 JButton 的 Tab 键的键绑定(bind)