鼠标毫无疑问
我正在尝试用基本的GUI作为夏季项目制作扫雷器,并希望使用Java Swing的Mouseadapter
@Override
public void mousePressed(MouseEvent e){ // shades surrounding tiles
for (int rowOff = tileLoc.row() - 1; rowOff < tileLoc.row() + 2; rowOff++) {
for (int colOff = (tileLoc.col() - 1); colOff < tileLoc.col() + 2; colOff++) {
// TODO add validTile()
allTiles[rowOff][colOff].setBackground(Color.GRAY);
}
}
}
@override
public void mouseReleased(MouseEvent e){
// undoes shading & clicks tile
}
(TileLoc是2个值,行和Col)< br> 该代码可以按我想要的方式工作,但是问题在于它也被鼠标列出了()
@Override
public void mouseClicked(MouseEvent e){ // 1 is left click, 3 is right click
switch (e.getButton()){
case 1:
//does board.leftClick()
case 3:
//does board.rightClick()
break;
default:
break;
}
}
如何使MouseClicked()不阴影并卸下瓷砖?
I'm trying to make minesweeper with a basic GUI as a summer project and want to be able to shade the surrounding tiles when you click and hold a tile using Java Swing's MouseAdapter
@Override
public void mousePressed(MouseEvent e){ // shades surrounding tiles
for (int rowOff = tileLoc.row() - 1; rowOff < tileLoc.row() + 2; rowOff++) {
for (int colOff = (tileLoc.col() - 1); colOff < tileLoc.col() + 2; colOff++) {
// TODO add validTile()
allTiles[rowOff][colOff].setBackground(Color.GRAY);
}
}
}
@override
public void mouseReleased(MouseEvent e){
// undoes shading & clicks tile
}
(TileLoc is a Record with 2 values, row and col)
This code works the way I want it to but the issue is that it's also getting called on the mouseClicked()
@Override
public void mouseClicked(MouseEvent e){ // 1 is left click, 3 is right click
switch (e.getButton()){
case 1:
//does board.leftClick()
case 3:
//does board.rightClick()
break;
default:
break;
}
}
How can I make the mouseClicked() not shade and unshade the tiles?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的方法是使用计时器变量。按下鼠标时,将当前时间保存到类变量
prestedtime = system.currenttimemillis();
:现在我们可以更改MousereREALEAD和MOUSECECLICKED方法的行为来说明此计时器,并采取行动因此:
请注意,取决于您使用的情况,如果您与多个函数/按钮共享变量,那么您可能需要将
persedtime
的值重置为-1在采取任何操作之前,请检查-1值。The best way to do this is with a timer variable. When the mouse is pressed save the current time to a class variable
pressedTime = System.currentTimeMillis();
:Now we can change the behaviour of the mouseReleased and mouseClicked methods to account for this timer, and take action accordingly:
Note that depending on the situation that you use this in, and if you share the variable with multiple functions/buttons, then you might want to reset the value of
pressedTime
to -1 after clicks, and check for a -1 value before taking any actions.MouseClick包括按下和释放同一位置的按钮。您想对MouseClicked做出反应 - 此时,您不知道用户何时发布按钮。
所以下定决心:您想立即做出反应吗?我认为这就是您已经拥有的。您想延迟反应吗? (这就是Sorifiend的建议。)如果用户很长时间后用户发布按钮会发生什么情况,但是在同一位置:是慢单击还是含义有所不同?
所有解决方案都需要在用户按下鼠标按钮时突出显示相邻字段的要求。
A mouseclick consists of pressing and releasing the button on the same location. You want to react on mouseclicked - at this point in time you do not know when the user releases the button.
So make up your mind: Do you want to react immediately? I think that's what you already have. Do you want to react delayed? (That's what sorifiend suggests.) And what happens if the user releases the button after a long time but at the same location: Was that a slow click, or does it mean something different?
All solutions work to the requirement that the adjoining fields are highlighted when the user presses the mouse button.