网格布局 +鼠标监听器
好吧,我有一个问题,我不知道如何知道在网格布局上单击了哪个单元格,有什么功能吗?
我在容器上有 10 行和 10 列的网格布局,我想要的是所有单元格的鼠标侦听器,因此当我单击单元格 (2,1) 时,它会因为鼠标侦听器而显示我正在单击哪个单元格。
有什么线索吗?预先非常感谢
Okay guys I have a problem, I dont know how know which cell was clicked on a grid layout, is there any function?
I have grid layout on container, for 10 rows and 10 columns, and what I want is a mouse listener to all the cells, so when I click cell (2,1) it would say which cell I am clicking because of the mouse listener.
Any clues? thanks alot in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 MouseListener 添加到使用 GridLayout 并保存网格中组件的容器。然后在 mousePressed 上使用 MouseEvent 对象(例如名为 myMouseEvent)来获取单击点,并调用 getComponentAt(myMouseEvent.getPoint); 来获取单击的组件。没有混乱没有大惊小怪。
例如:
Add a MouseListener to the Container that uses GridLayout and that holds the components in the grid. Then on mousePressed use the MouseEvent object, say called myMouseEvent, to get the point of the click and call
getComponentAt(myMouseEvent.getPoint);
to get the clicked component. No muss no fuss.For example:
您可以使用容器的宽度和高度,获取容器的宽度和高度,然后将其除以单元格数量并找到单元格的范围(2,1),例如,如果容器的宽度和高度等于100,行数,列=10*10,则单元格(2,1)的左坐标为x=10,y=0,通过mouseListener类的方法getX()和getY()找到鼠标的位置点击,那么如果鼠标在单元格(2,1)范围内的x和y位置可以知道它点击了单元格(2,1)。

You can work with the width an height of your container, get the width an height of container and divided it to the number of cells and find the range of the cell(2,1),for example if width and height of container equal to 100 and number of rows,column=10*10 then the left coordinate of cell(2,1) is x=10,y=0 and by method getX() and getY() of the mouseListener class you find the location of mouse clicked, then if the x and y of mouse location in the range of cell(2,1) you can know that it clicked on the cell(2,1).

递归地将鼠标监听器添加到 UI 中的所有组件并调试...
recursively add a mouselistener to all Components in your UI and debug...