简体   繁体   English

如何在JPanel GridLayout上进行鼠标进入和鼠标退出?

[英]How to mouse enter and mouse exit on JPanel GridLayout?

I have problem how to make mouse exit and mouse enter in GridLayout on JPanel which contain 2 Panels in 1 cell. 我在如何使JPanel上的GridLayout鼠标退出和鼠标进入中存在问题,该面板在1个单元格中包含2个面板。

I don't know how to know which cell was enter on a grid layout, is there any function? 我不知道如何在网格布局中输入哪个单元格,有什么功能吗?

I have grid layout on container, for 5 rows and 3 columns, and what I want is a mouse listener to all the cells, so when I enter cell it would say which cell I am entering because of the mouse listener. 我在容器上有5行3列的网格布局,我想要的是对所有单元格的鼠标侦听器,因此当我输入单元格时,它会说由于鼠标侦听器而要输入的单元格。

Any clues? 有什么线索吗?

MouseEvent.getComponent() will return the component which generated the event. MouseEvent.getComponent()将返回生成事件的组件。 If the code adds a mouse listener to each of the panels in the grid, it is easy to find out which one fired the event. 如果代码将鼠标侦听器添加到网格中的每个面板,则很容易找出触发该事件的面板。

import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class MousePanelArrayTest {

    private final JComponent ui = new JPanel(new BorderLayout(4, 4));
    MouseListener mouseListener;

    MousePanelArrayTest() {
        initUI();
    }

    public final void initUI() {
        mouseListener = new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent e) {
                Component c = e.getComponent();
                c.setBackground(Color.BLACK);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                Component c = e.getComponent();
                c.setBackground(Color.WHITE);
            }
        };

        ui.setBorder(new EmptyBorder(4, 4, 4, 4));
        JPanel gridPanel = new JPanel(new GridLayout(0, 5, 4, 4));
        ui.add(gridPanel);
        for (int ii=0; ii<20; ii++) {
            gridPanel.add(getPanel());
        }
    }

    private JPanel getPanel() {
        JPanel p = new JPanel();
        p.addMouseListener(mouseListener);
        p.setBorder(new EmptyBorder(10, 20, 10, 20));

        return p;
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception useDefault) {
            }
            MousePanelArrayTest o = new MousePanelArrayTest();

            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);

            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());

            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}````

One way to do this would be to get the position of the mouse when it enters the grid layout and calculate the cell that this position belongs to. 一种方法是在鼠标进入网格布局时获取鼠标的位置,然后计算该位置所属的单元格。 Becuase the grid layout only alows cells with the same size this could be easily calculated. 因为网格布局仅允许使用相同大小的单元格,所以可以轻松计算出该大小。 In pseudocode: 用伪代码:

event = the mouse event
position = event.getPosition()
panelPosition = position of the panel that includes the grid layout
relativePosition = [position.x - panePosition.x, position.y - panePosition.y]

//get width and height of the panel that includes the gridlayout
int width = pane.width 
int height = pane.height

int numRows = gridlayout.getRows()
int numCols = gridlayout.getCols()

int cellx = relativePosition.x / (width / numRows)
int celly = relativePosition.y / (height / numCols)
//the cell where the mouse is is [cellx, celly]

Here the MouseListener needs to be added to the panel that includes the GridLayout , because you can't add a MouseListener to the layout itselves (but the layout will have the same size like the panel, so it doesn't realy matter). 这里,需要将MouseListener添加到包含GridLayout的面板中,因为您不能将MouseListener添加到布局本身(但是布局将具有与面板相同的大小,所以这并不重要)。

Another way would be to add a panel into every cell of the gridlayout and check whether the mouse entered one of this panels (of which you know the cell, because you added them to the layout). 另一种方法是将一个面板添加到gridlayout的每个单元中,并检查鼠标是否进入了其中一个面板(您知道该单元,因为您已将它们添加到布局中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM