检查 java swing GUI 中的一系列点击

发布于 2024-08-23 01:57:17 字数 161 浏览 8 评论 0原文

我正在为国际象棋游戏开发 GUI,我想知道是否有任何方法可以检查一系列点击,例如:用户点击 jPanel,然后用户点击有效移动数组中存在的另一个 jPanel。我知道我可以使用变量来存储某种状态,例如“isSquareClicked = true”或其他东西,但我宁愿不这样做,除非这是唯一的方法......

I'm working on a GUI for a chess game and I was wondering if there's any way to check for a series of clicks, for example: user clicks on jPanel THEN user clicks on another jPanel that exists in valid move array. I know I could use a variable to store some kind of state like "isSquareClicked = true" or something but I'd rather not unless that's the only way...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

温折酒 2024-08-30 01:57:17

我不认为使用 JPanel 有什么问题。这是我的实现:

首先是一个 ChessSquare,它代表棋盘上的一个单元格:

public class ChessSquare extends JPanel{
    int x,y;

    public ChessSquare(int x, int y){
        super();
        this.setPreferredSize(new Dimension(50,50));
        this.setBorder(BorderFactory.createLineBorder(Color.black));
        this.x = x;
        this.y = y;
    }
}

现在是主板面板:

public class ChessPanel extends JPanel{
    JPanel positions[][] = new JPanel[8][8];
    ChessSquare move[] = new ChessSquare[2];

    public ChessPanel(){
        initComponents();
    }

    private void initComponents(){
        setLayout(new GridLayout(8,8));

        for(int i=0;i<positions.length;i++){
            for(int j=0;j<positions[i].length;j++){
                ChessSquare square = new ChessSquare(i,j);
                square.addMouseListener(new MouseListener(){
                    public void mouseClicked(MouseEvent me) {
                        ChessSquare cs = (ChessSquare)me.getComponent();
                        if(isValidMove(cs)){

                            System.out.println("Valid move!");
                            System.out.println("x1: "+move[0].x+" y1: "+move[0].y);
                            System.out.println("x2: "+move[1].x+" y2: "+move[1].y);
                            System.out.println("");

                            resetMove();
                        }
                    }

                    //Other mouse events

                });
                positions[i][j] = square;
                add(square);
            }
        }
    }

    private boolean isValidMove(ChessSquare square){
        //here you would check if the move is valid.
        if(move[0] == null){
            move[0] = square;
            return false; //first click
        }else{
            move[1] = square;
        }

        //Other chess rules go here...
        return true;
    }

    private void resetMove(){
        move = new ChessSquare[2];
    }
}

我们保留一个 JPanel 矩阵来表示棋盘,并使用 ChessSquare 数组来表示当前的移动。在 isValidMove() 中,我们检查当前移动是否完成(两个方块都已被单击,因此移动数组已经有一个元素)。一旦移动完成,我们将重置移动并重新开始。

I don't see anything wrong with using JPanels. Here's my implementation:

First a ChessSquare, this represents one cell on the board:

public class ChessSquare extends JPanel{
    int x,y;

    public ChessSquare(int x, int y){
        super();
        this.setPreferredSize(new Dimension(50,50));
        this.setBorder(BorderFactory.createLineBorder(Color.black));
        this.x = x;
        this.y = y;
    }
}

Now the main board panel:

public class ChessPanel extends JPanel{
    JPanel positions[][] = new JPanel[8][8];
    ChessSquare move[] = new ChessSquare[2];

    public ChessPanel(){
        initComponents();
    }

    private void initComponents(){
        setLayout(new GridLayout(8,8));

        for(int i=0;i<positions.length;i++){
            for(int j=0;j<positions[i].length;j++){
                ChessSquare square = new ChessSquare(i,j);
                square.addMouseListener(new MouseListener(){
                    public void mouseClicked(MouseEvent me) {
                        ChessSquare cs = (ChessSquare)me.getComponent();
                        if(isValidMove(cs)){

                            System.out.println("Valid move!");
                            System.out.println("x1: "+move[0].x+" y1: "+move[0].y);
                            System.out.println("x2: "+move[1].x+" y2: "+move[1].y);
                            System.out.println("");

                            resetMove();
                        }
                    }

                    //Other mouse events

                });
                positions[i][j] = square;
                add(square);
            }
        }
    }

    private boolean isValidMove(ChessSquare square){
        //here you would check if the move is valid.
        if(move[0] == null){
            move[0] = square;
            return false; //first click
        }else{
            move[1] = square;
        }

        //Other chess rules go here...
        return true;
    }

    private void resetMove(){
        move = new ChessSquare[2];
    }
}

We keep a JPanel matrix to represent the board, and ChessSquare array to represent the current move. In isValidMove() we check to see if the current move is complete (both squares have been clicked, thus the move array already has one element). Once a move is complete, we reset the move and start again.

坦然微笑 2024-08-30 01:57:17

我同意 imp - 您可能想要一个 JPanel 并在该面板上绘制所有内容。

话虽这么说,如果有人已经使用 8x8 JPanel 实现了一个棋盘并告诉我使用它,我可能会尝试将 8x8 JPanel 放入 JLayeredPane 中,然后在所有内容之上放置一个透明 JPanel处理所有鼠标点击。

尽管如此,这种方法仍然需要您进行点算术来找出正在单击的单元格,而且我猜测使用 8x8 JPanel 的目的是您希望首先避免进行这种算术。

I agree with imp - You probably want to have a single JPanel and draw everything on that one panel.

That being said, if someone had already implemented a chessboard with 8x8 JPanels and told me to use it, I might try putting the 8x8 JPanels in a JLayeredPane and then putting a single transparent JPanel on top of everything to handle all the mouse clicks.

Still, that approach will require you to do Point arithmetic to figure out what cell is being clicked on, and I'm guessing that the point of using 8x8 JPanels was that you wanted to avoid doing that kind of arithmetic in the first place.

墨落成白 2024-08-30 01:57:17

据我所知,Java中没有这样的东西。

但是:

1)据我了解,您使用 8x8 JPanels 的字段来创建游戏字段?恕我直言,这是个坏方法。如果我是你,我会使用一个面板来创建字段 - 通过在其上绘制所有内容(单元格、图形等)。这更简单、创建更快、更容易使用。

2)回到你的问题。如果您有一个字段面板 - 您只需要记住两对坐标:第一次点击在哪里,第二次点击在哪里。在您的情况下 - 单击面板上的 2 个指针就足够了。 ;)

希望这有帮助:)

As I know, there is no such things in Java.

But:

1) As I understand, You use field of 8x8 JPanels to create field for game? IMHO this is bad way. If I were you, I would use one Panel for creating field - by painting all on it(cells, figures, etc). This is more simpler, faster to create, easier to work with.

2) Returning to your question. If you have one panel for field - you only need to remember 2 pairs of coordinates: where was first click, and where was second. In Your case - 2 pointers on panels wich was clicked will be enough. ;)

Hope this help :)

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