我正在尝试创建一个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");
}
});
}
}
答案 0 :(得分:14)
您可以使用鼠标Entered
和Exited
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"));
}
});
答案 1 :(得分:1)
我曾经写过一个自定义的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();
}
});
}
}
好的一点是你可以调整代码来改变按钮的背景颜色,这也是一种动画方式。
答案 2 :(得分:0)
哇。老问题,我知道,但是......
要更改背景,请使用:
b1.setBackground(new java.awt.Color(r, g, b));
在actionListener中。
对于翻转效果,您可以使用:
b1.setRolloverEnabled(true);
但您需要提供按钮的图标以便在其间切换。
否则,对于其他悬停效果,您需要使用mouseListener。