簡體   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