如何在鼠标悬停时突出显示 JPanel?

How can I highlight JPanels on mouse hover?

我有 16 个 Jpanel,当我将鼠标悬停在它们上面时,我想突出显示它们。我匿名创建了 JPanel,然后将它们添加到 parent,并为每个 JPanel 添加了一个 MouseListener。然后我向 parent 添加了一个 MouseListener。问题是,现在它只是突出显示 parent。我该如何解决这个问题?

注意: 有时 JFrame 不显示任何内容 - 您只需保持 运行 它直到显示(通常需要 2-3 次尝试)。如果在超过 5 次尝试后仍然无法正常工作,请发表评论。

HighlightJPanels(创建 JFrame、容器和 children,并添加 MouseListeners)

public class HighlightJPanels extends JFrame{
    private static final long serialVersionUID = 7163215339973706671L;
    private static final Dimension containerSize = new Dimension(640, 477);
    private JLayeredPane layeredPane;
    static JPanel container;

    public HighlightJPanels() {
        super("Highlight Test");
        setSize(640, 477);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);

        layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(containerSize);
        getContentPane().add(layeredPane);

        createContainer();

        layeredPane.add(container, JLayeredPane.DEFAULT_LAYER);

        createChildren(4, 4);

        container.addMouseMotionListener(new HighlightJPanelsContainerMouseListener());
    }

    private void createChildren(int columns, int rows){
        for (int i = 0; i < columns; i++){
            for (int j = 0; j < rows; j++){
                JPanel child = new JPanel(new BorderLayout());
                child.setBackground(Color.LIGHT_GRAY);
                child.addMouseListener(new HighlightJPanelsMouseListeners());
                container.add(child);
            }
        }
    }

    private JPanel createContainer(){
        container = new JPanel();
        container.setLayout(createLayout(4, 4, 1, 1));
        container.setPreferredSize(containerSize);
        container.setBounds(0, 0, containerSize.width, containerSize.height);
        return container;
    }

    private GridLayout createLayout(int rows, int columns, int hGap, int vGap){
        GridLayout layout = new GridLayout(rows, columns);
        layout.setHgap(hGap);
        layout.setVgap(vGap);
        return layout;
    }

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

HighlightJPanelsChildMouseListeners(创建将添加到 children 的 MouseListeners)

public class HighlightJPanelsChildMouseListeners implements MouseListener{
    private Border grayBorder = BorderFactory.createLineBorder(Color.DARK_GRAY);

    public HighlightJPanelsChildMouseListeners() {
    }

    public void mouseEntered(MouseEvent e) {
        Component comp = HighlightJPanels.container.findComponentAt(HighlightJPanelsContainerMouseListener.eX, HighlightJPanelsContainerMouseListener.eY);
        JPanel parent = (JPanel) comp;
        parent.setBorder(grayBorder);
        parent.revalidate();
    }

    public void mouseExited(MouseEvent e) {
        Component comp = HighlightJPanels.container.findComponentAt(HighlightJPanelsContainerMouseListener.eX, HighlightJPanelsContainerMouseListener.eY);
        JPanel parent = (JPanel) comp;
        parent.setBorder(null);
        parent.revalidate();
    }

    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    public void mouseClicked(MouseEvent e) {}
}

HighlightJPanelsContainerMouseListener(创建将添加到容器中的 MouseListener)

public class HighlightJPanelsContainerMouseListener implements MouseMotionListener{
    static int eX;
    static int eY;

    public void mouseDragged(MouseEvent e) {}

    public void mouseMoved(MouseEvent e) {
        eX = e.getX();
        eY = e.getY();
    }
}

问题是由您如何在这一行中找到要突出显示的 JPanel 引起的:

Component comp = HighlightJPanels.container.findComponentAt(HighlightJPanelsContainerMouseListener.eX, HighlightJPanelsContainerMouseListener.eY);

幸运的是,已经有一个函数可以满足您的需求。您可以只在事件上使用 getSource(),它会告诉您要突出显示哪个面板。因此,将您的功能更改为:

public void mouseEntered(MouseEvent e) {
    JPanel parent = (JPanel)e.getSource();
    parent.setBorder(grayBorder);
    parent.revalidate();
}

并对 mouseExited 执行相同的操作,您会看到它突出显示了正确的面板。这将消除对 HighlightJPanelsContainerMouseListener.

的需要