java – 如何将悬停效果放在jbutton上?

前端之家收集整理的这篇文章主要介绍了java – 如何将悬停效果放在jbutton上?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个 Java桌面应用程序,我使用两个按钮.我想在这些按钮中添加悬停效果.我想:当我点击任何按钮时,它应该改变它的背景颜色.

我怎样才能实现它?

这是我的代码

  1. public class Party1Party2 extends JFrame
  2. {
  3. JButton b1;
  4. JButton b2;
  5. Container pane;
  6.  
  7. public Party1Party2()
  8. {
  9. getContentPane().setBackground(new java.awt.Color(255,255,255));
  10.  
  11. b2.addActionListener(new ActionListener()
  12. {
  13. public void actionPerformed(ActionEvent ae)
  14. {
  15. JOptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");
  16. }
  17. });
  18.  
  19. b1.addActionListener(new ActionListener()
  20. {
  21. public void actionPerformed(ActionEvent ae)
  22. {
  23. JOptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");
  24.  
  25. }
  26. });
  27. }
  28. }

解决方法

你可以使用鼠标输入和退出JButton,并做任何你想要的.

例如:

  1. jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
  2. public void mouseEntered(java.awt.event.MouseEvent evt) {
  3. jButton1.setBackground(Color.GREEN);
  4. }
  5.  
  6. public void mouseExited(java.awt.event.MouseEvent evt) {
  7. jButton1.setBackground(UIManager.getColor("control"));
  8. }
  9. });

猜你在找的Java相关文章