繁体   English   中英

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

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

我在如何使JPanel上的GridLayout鼠标退出和鼠标进入中存在问题,该面板在1个单元格中包含2个面板。

我不知道如何在网格布局中输入哪个单元格,有什么功能吗?

我在容器上有5行3列的网格布局,我想要的是对所有单元格的鼠标侦听器,因此当我输入单元格时,它会说由于鼠标侦听器而要输入的单元格。

有什么线索吗?

MouseEvent.getComponent()将返回生成事件的组件。 如果代码将鼠标侦听器添加到网格中的每个面板,则很容易找出触发该事件的面板。

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

一种方法是在鼠标进入网格布局时获取鼠标的位置,然后计算该位置所属的单元格。 因为网格布局仅允许使用相同大小的单元格,所以可以轻松计算出该大小。 用伪代码:

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]

这里,需要将MouseListener添加到包含GridLayout的面板中,因为您不能将MouseListener添加到布局本身(但是布局将具有与面板相同的大小,所以这并不重要)。

另一种方法是将一个面板添加到gridlayout的每个单元中,并检查鼠标是否进入了其中一个面板(您知道该单元,因为您已将它们添加到布局中)。

暂无
暂无

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

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