字段中的数据错误

发布于 2024-11-08 19:52:31 字数 2428 浏览 0 评论 0原文

这是我的程序中的类片段(我删除了一些不重要的代码)。首先,程序总是调用方法firstSet()来设置JPanel(board)上的所有JLabels(players)。然后,当我单击 JLabel 时,mouseClicked() 运行方法 findPlayer(),因此我知道谁选择了哪个 JLabel。到这一刻一切都还好。

当我开始模拟时,问题就开始了 - 方法 simulationStart() 运行 10 次。此后,当我单击播放器时,cmd 行显示:我找不到它:(。我编写了一些 system.out 来找出问题所在。问题出在字段 locationX 和 locationY,我不知道为什么它们在 findPlayer()simulationStart() 中不同。

public class setBoard extends JFrame implements MouseListener {

    int[] locationX = new int[100];
    int[] locationY = new int[100];

    public void setLocation() {
        for (int i = 0; i < 100; i++) {
            locationX[i] = (int) (random() * (sizeX - xy));
            locationY[i] = (int) (random() * (sizeY - xy));
        }
    }

    public ArrayList<JLabel> firstSet() {
        setLocation();

        for (int i = 0; i < 100; i++) {
            player = new JLabel();

            //
            //some code to JLabel set
            //

            playerList.add(player);
            player.setBounds(locationX[i], locationY[i]);
            player.addMouseListener(this);
        }

        return playerList;
    }

    public ArrayList<JLabel> simulationStart(ArrayList<JLabel> playerList) {
        this.playerList = playerList;

        setLocation();

        for (int i = 0; i < 100; i++) {
            playerList.get(i).setBounds(locationX[i], locationY[i]);
            playerList.add(playerList.get(i));
        }
        return playerList;
    }

    public void mouseClicked(MouseEvent e) {     
        //
        //a lot of code to  search coursorX and coursorY, this works good
        //

        //if left mouse button 
        if (e.getButton() == 1) {
            final int playerNr = findPlayer(coursorX, coursorY);
            if (playerNr == -1) {
                System.out.println("I can`t find it :( ");
            }
            else{
                //
                //code to do when it find player
                //
            }
        }
    }

    //
    //
    //

    private int findPlayer(int x, int y) {
        int playerNr = -1;
        for (int i = 0; i < 100; i++) {
            if (x == locationX[i] && y == locationY[i]) {
                playerNr = i;
            }
        }
        return playerNr;
    }

}

This is fragment of class in my program (I deleted some not important code). At First, program always calls method firstSet() which sets all JLabels(players) on JPanel(board). Then when I click on JLabel, mouseClicked() runs method findPlayer(), so I know which JLabel whose chose. To this moment everything is OK.

Problem begins when I start simulation - method simulationStart() run 10 times. After this when I click player, cmd line shows: I can't find it :(. I wrote some system.out to find out what is wrong. Problem is in fields locationX and locationY, I don't know why they are different in findPlayer() and simulationStart().

public class setBoard extends JFrame implements MouseListener {

    int[] locationX = new int[100];
    int[] locationY = new int[100];

    public void setLocation() {
        for (int i = 0; i < 100; i++) {
            locationX[i] = (int) (random() * (sizeX - xy));
            locationY[i] = (int) (random() * (sizeY - xy));
        }
    }

    public ArrayList<JLabel> firstSet() {
        setLocation();

        for (int i = 0; i < 100; i++) {
            player = new JLabel();

            //
            //some code to JLabel set
            //

            playerList.add(player);
            player.setBounds(locationX[i], locationY[i]);
            player.addMouseListener(this);
        }

        return playerList;
    }

    public ArrayList<JLabel> simulationStart(ArrayList<JLabel> playerList) {
        this.playerList = playerList;

        setLocation();

        for (int i = 0; i < 100; i++) {
            playerList.get(i).setBounds(locationX[i], locationY[i]);
            playerList.add(playerList.get(i));
        }
        return playerList;
    }

    public void mouseClicked(MouseEvent e) {     
        //
        //a lot of code to  search coursorX and coursorY, this works good
        //

        //if left mouse button 
        if (e.getButton() == 1) {
            final int playerNr = findPlayer(coursorX, coursorY);
            if (playerNr == -1) {
                System.out.println("I can`t find it :( ");
            }
            else{
                //
                //code to do when it find player
                //
            }
        }
    }

    //
    //
    //

    private int findPlayer(int x, int y) {
        int playerNr = -1;
        for (int i = 0; i < 100; i++) {
            if (x == locationX[i] && y == locationY[i]) {
                playerNr = i;
            }
        }
        return playerNr;
    }

}

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

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

发布评论

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

评论(1

半步萧音过轻尘 2024-11-15 19:52:31

每当您调用 simulationStart 时,您都会再次将玩家添加到玩家列表中。所以玩家数量超过 100。为什么不尝试打印出playerList 的大小并检查它是否是您期望的?

我认为您可能需要删除以下语句: playerList.add(playerList.get(i));

Whenever you call simulationStart you are adding players to the playerList again. So the number of players goes above 100. Why don't you try printing out the size of playerList and check if it is what you expect?

I think you may need to remove the statement: playerList.add(playerList.get(i));

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