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

时间:2016-11-23 16:07:39

标签: java mouseevent mouseover

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

注意:有时JFrame没有显示任何内容 - 您只需要继续运行它(通常需要2-3次尝试)。在> 5次尝试后仍然无效的评论。

HighlightJPanels (创建JFrame,容器和子项,并添加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 (创建将添加到子项中的MouseListener)

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();
    }
}

1 个答案:

答案 0 :(得分:5)

问题是由于您在此行找到要突出显示的public interface IFoo { bool IsCorrect(IFoo foo); void DoSomething(IFoo foo); } List<IFoo> listA; //assume that contains items List<IFoo> listB; //assume that contains items foreach (var a in listA) { foreach (var b in listB) { if (b.IsCorrect(a)) { b.DoSomething(a); } } } 的方式:

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