扫雷动作事件
有没有办法使某些事件操作特定于鼠标左键和右键单击?
我正在创建一个扫雷图形用户界面,因此当左键单击一个方块时,它会被发现,&当右键单击时,它将被标记。
我不确定如何从语法上检查 this &在图上找不到它。
感谢您的帮助!
Is there a way to make certain event actions specific to left and right mouse clicks?
I'm creating a minesweeper gui, so when a square is left-clicked it will be uncovered, & when it's right-clicked it will be flagged.
I wasn't sure how to syntactically check for this & couldn't find it on the tut.
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我决定尝试一下,尝试创建一个简单的扫雷应用程序,一个没有计时器或重置(尚未)的应用程序,但它是功能性的,并且使用 GUI 单元类和非 GUI 模型类(它可以'不得复制并用于 Java 介绍作业)。
编辑 1:现在具有重置功能:
MineSweeper.java:保存 main 方法并启动 JFrame
MineCellGrid.java:显示地雷单元格网格并将它们一起计时的类。
MineCellGridModel.java:MineCellGrid 的非 GUI 模型
MineCell.java:我开始使用的类。使用模型类作为其非 GUI 核心。
MineCellModel.java:矿井单元的非 GUI 模型
下面是将整个程序组合成一个 MCVE 文件,MineSweeper.java:
I decided to give it a go, to try to create a simple Mine Sweeper application, one without a timer or reset (yet), but that is functional and uses both a GUI cell class and a non-GUI model class (it can't be copied and used in for intro to Java homework).
Edit 1: now has reset capability:
MineSweeper.java: holds the main method and starts the JFrame
MineCellGrid.java: the class that displays the grid of mine cells and times them all together.
MineCellGridModel.java: the non-GUI model for the MineCellGrid
MineCell.java: the class that I started on. Uses the model class as its non-GUI nucleus.
MineCellModel.java: the non-GUI model for the mine cell
Here's the whole program combined into a single MCVE file, MineSweeper.java:
如果您使用 swing 那么
实现 MouseListener 无组件。然后在实现的方法中,您有一个
MouseEvent
对象,它有一个getButton()
方法,可以告诉您按下了哪个鼠标。编辑
OP提出了以下问题,但现在将其删除。
您可以为此打开一个 JDialog。If you are using swing then
Implement a MouseListener no component. Then in implemented method you have a
MouseEvent
object which has agetButton()
method which tells you which mouse is pressed.Edit
OP has asked following question but now removed it.
You can open a JDialog for this.您可能对 java.awt.event 的 MouseEvent 类感兴趣。 此处
You may be interested in the MouseEvent Class of java.awt.event. here