我正在尝试创建一个使用两个按钮的 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");
}
});
}
}
最佳答案
您可以使用鼠标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"));
}
});
关于java - 如何在 jbutton 上设置悬停效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22638926/