鼠标毫无疑问

发布于 2025-02-09 00:22:54 字数 1366 浏览 5 评论 0原文

我正在尝试用基本的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宁愿没拥抱 2025-02-16 00:22:54

最好的方法是使用计时器变量。按下鼠标时,将当前时间保存到类变量prestedtime = system.currenttimemillis();

long pressedTime = -1;

public void mousePressed(MouseEvent e){ // shades surrounding tiles
    pressedTime = System.currentTimeMillis();
    //Your code here...
    //...
}

现在我们可以更改MousereREALEAD和MOUSECECLICKED方法的行为来说明此计时器,并采取行动因此:

@override
public void mouseReleased(MouseEvent e){ 
     //Find the time difference
     long timeDifference = System.currentTimeMillis() - pressedTime;

     //If the held time is long enough do the held action
     if(timeDifferenec > yourRequiredTime){
         //Do something
     }

    //Else if the click was shorter than your hold time then manage your click like normal:
     else{
         switch (e.getButton()){
            case 1:
                 //does board.leftClick()
            case 3:
                //does board.rightClick()
                break;
            default:
                break;
     }
}

//Do nothing in the mouseClicked event, we can manage this entirely in the mouse released event
public void mouseClicked(MouseEvent e){
     //Nothing here
}

请注意,取决于您使用的情况,如果您与多个函数/按钮共享变量,那么您可能需要将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();:

long pressedTime = -1;

public void mousePressed(MouseEvent e){ // shades surrounding tiles
    pressedTime = System.currentTimeMillis();
    //Your code here...
    //...
}

Now we can change the behaviour of the mouseReleased and mouseClicked methods to account for this timer, and take action accordingly:

@override
public void mouseReleased(MouseEvent e){ 
     //Find the time difference
     long timeDifference = System.currentTimeMillis() - pressedTime;

     //If the held time is long enough do the held action
     if(timeDifferenec > yourRequiredTime){
         //Do something
     }

    //Else if the click was shorter than your hold time then manage your click like normal:
     else{
         switch (e.getButton()){
            case 1:
                 //does board.leftClick()
            case 3:
                //does board.rightClick()
                break;
            default:
                break;
     }
}

//Do nothing in the mouseClicked event, we can manage this entirely in the mouse released event
public void mouseClicked(MouseEvent e){
     //Nothing here
}

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.

酒儿 2025-02-16 00:22:54

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文