我可以在 Java JFrame 中将多个相同类型的动作事件合并为一个吗?

Can I join several action events of the same type into one in a Java JFrame?

我有一个 java 项目,我试图在其中制作类似于 CSS 中的悬停效果,每次鼠标经过 jlabel 时都会更改颜色(图标),但是我意识到我正在使用几种方法来做同样的事情。

我的问题是是否有可能将所有这些统一到一个方法中,或者是否有更简单的方法来使用库或类似的东西来制作这种动画。

private void lblPersonalizarMouseExited(java.awt.event.MouseEvent evt) {                                            
    lblPersonalizar.setIcon(icono_personalizari);
}                                           

private void lblPersonalizarMouseEntered(java.awt.event.MouseEvent evt) {                                             
    lblPersonalizar.setIcon(icono_personalizara);
}                                            

private void lblNuevaCompraMouseExited(java.awt.event.MouseEvent evt) {                                           
    lblNuevaCompra.setIcon(icono_comprai);
}                                          

private void lblNuevaCompraMouseEntered(java.awt.event.MouseEvent evt) {                                            
    lblNuevaCompra.setIcon(icono_compraa);
}                                           

private void lblUsuarioMouseEntered(java.awt.event.MouseEvent evt) {                                        
    lblUsuario.setIcon(icono_usuarioa);
}                                       

private void lblUsuarioMouseExited(java.awt.event.MouseEvent evt) {                                       
    lblUsuario.setIcon(icono_usuarioi);
}                                      

private void lblFacturasMouseEntered(java.awt.event.MouseEvent evt) {                                         
    lblFacturas.setIcon(icono_facturasa);
}                                        

private void lblFacturasMouseExited(java.awt.event.MouseEvent evt) {                                        
    lblFacturas.setIcon(icono_facturasi);
}                                       

private void lblMaterialesMouseEntered(java.awt.event.MouseEvent evt) {                                           
    lblMateriales.setIcon(icono_materialesa);
}                                          

private void lblMaterialesMouseExited(java.awt.event.MouseEvent evt) {                                          
    lblMateriales.setIcon(icono_materialesi);
}                                         

private void lblAyudaMouseEntered(java.awt.event.MouseEvent evt) {                                      
    lblAyuda.setIcon(icono_ayudaa);
}                                     

private void lblAyudaMouseExited(java.awt.event.MouseEvent evt) {                                     
    lblAyuda.setIcon(icono_ayudai);
}   
                             

JButton支持图像翻转,这将是一个更简单的解决方案

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws IOException {
            setLayout(new GridBagLayout());
            setBackground(Color.BLACK);

            JButton bagButton = makeButton(new ImageIcon(ImageIO.read(getClass().getResource("/images/Bag.png"))), new ImageIcon(ImageIO.read(getClass().getResource("/images/Bag-Selected.png"))));
            JButton editButton = makeButton(new ImageIcon(ImageIO.read(getClass().getResource("/images/Edit.png"))), new ImageIcon(ImageIO.read(getClass().getResource("/images/Edit-Selected.png"))));
            JButton settingsButton = makeButton(new ImageIcon(ImageIO.read(getClass().getResource("/images/Settings.png"))), new ImageIcon(ImageIO.read(getClass().getResource("/images/Settings-Selected.png"))));

            add(bagButton);
            add(editButton);
            add(settingsButton);

            bagButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Bag was selected");
                }
            });

            editButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Edit was selected");
                }
            });

            settingsButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Settings was selected");
                }
            });
        }

        protected JButton makeButton(Icon icon, Icon rollOverIcon) {
            JButton button = new JButton();
            button.setIcon(icon);
            button.setRolloverIcon(rollOverIcon);
            button.setRolloverEnabled(true);
            button.setBorderPainted(false);
            button.setFocusPainted(false);
            return button;
        }

    }
}

一定要看看 How to Use Buttons, Check Boxes, and Radio Buttons