Java 如何在jbutton上放置悬停效果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22638926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to put Hover effect on jbutton?
提问by user3456343
I am trying to create a Java Desktop application where I am using two buttons. I want to add hover effect in those buttons. I want: When I click any button it should change its background color.
我正在尝试创建一个 Java 桌面应用程序,其中我使用了两个按钮。我想在这些按钮中添加悬停效果。我想要:当我点击任何按钮时,它应该改变它的背景颜色。
How can I achieve it?
我怎样才能实现它?
Here is my code:
这是我的代码:
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");
}
});
}
}
采纳答案by Salah
You can use moused Entered
and Exited
the JButton
, and do what ever you want.
您可以使用滑鼠Entered
和Exited
的JButton
,和你什么都想要。
For Example:
例如:
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"));
}
});
回答by Aman Agnihotri
I once wrote a custom JButton which used to change its transparency level when the mouse was hovered over it through animation. Here's the code for that button:
我曾经写过一个自定义 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();
}
}
}
And here's a quick demonstration of the HoverButton
:
这是一个快速演示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();
}
});
}
}
Good thing is that you can tweak the code to change the background color of the button as well, and that too, in an animated way.
好消息是,您还可以调整代码以更改按钮的背景颜色,而且还可以以动画方式进行更改。
回答by Kevin Donaldson
Wow. Old question, I know, but...
哇。老问题,我知道,但是...
To change the background, use:
要更改背景,请使用:
b1.setBackground(new java.awt.Color(r, g, b));
in the actionListener.
在动作监听器中。
For a rollover effect, you can use:
对于翻转效果,您可以使用:
b1.setRolloverEnabled(true);
but you will need to supply icons for your buttons to flip between.
但是您需要为按钮提供图标以在它们之间切换。
Otherwise, for other hover effects, you do need to use a mouseListener.
否则,对于其他悬停效果,您确实需要使用 mouseListener。