简体   繁体   English

Swing:在我将鼠标悬停在组件上之前,组件不会消失

[英]Swing: Component wont dissapear until I hover over the component

I am developing a small game in java swing as a small school project.我正在用 java swing 作为一个小型学校项目开发一个小游戏。 I am done with all the logic and GUI.我已经完成了所有的逻辑和 GUI。

The game(Snakes and Stairs) has 36 squares(JButtons) and each of those have Jpanels inside that can be used to put the players piece at(JButtons).游戏(Snakes and Stairs)有 36 个方格(JButtons),每个方格里面都有 Jpanels,可以用来放置玩家的棋子(JButtons)。 In other words, I have 36 buttons which all have Jpanels inside them, and all the JPanels can reside buttons.换句话说,我有 36 个按钮,它们都包含 Jpanel,并且所有 JPanel 都可以驻留按钮。 On each square I have put an action listener that checks whose turn it is, if the player can move here, and moves the players button to that square only if those conditions(and more ofcourse) are true.在每个方格上,我都放置了一个动作侦听器,用于检查轮到谁的回合,玩家是否可以在这里移动,并且仅当这些条件(当然还有更多)为真时才将玩家按钮移动到该方格。

Now comes the buggy part.现在是错误的部分。 When a players piece moves, it appears on the new square and the old one.当玩家棋子移动时,它会出现在新方格旧方格上。 The piece only dissapears from the old square if I hover over it.如果我将鼠标悬停在它上面,这件作品只会从旧广场上消失。

Some code that might help understand:一些可能有助于理解的代码:

//this happens in another function. I only show this, because i think this is the only part relevant from the function
spots[i][j].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        //EventQueue.invokeLater(()->setGameSpotsAction(f,p,spotNr));
                        setGameSpotsAction(f,p,spotNr);
                    }
                });


//action to do when a spot/square is pressed
    public void setGameSpotsAction(JFrame f, JPanel p, int nr) {//nr is the spot where the piece should go
        if(nr == X*Y && playerPosition[playerTurn] + latestRoll == nr){//if dice is rolled
            f.remove(p);
            winnerwinnerchickendinner.setText(namesArr[playerTurn]+" WON!!!!!!");
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx=0;gbc.gridy=0;panel.add(winnerwinnerchickendinner,gbc);
            f.getContentPane().add(panel);
        } else if (latestRoll >= 2 && nr <= X*Y && playerPosition[playerTurn] + latestRoll == nr) {//
                int sot = snakeOrStairs[playerPosition[playerTurn] + latestRoll];//sot stands for Snake Or sTair
                //if just regular square/spot
                if(playerPosition[1] != playerPosition[2]){//if player moves and the previous spot is empty, make panel invisible.
                    spotPanels[playerPosition[playerTurn]].setVisible(false);
                }
                if (sot == 0) {
                    playerPosition[playerTurn] += latestRoll;//button has new position
                    movePlayerButton(nr);
                    //EventQueue.invokeLater(()->{movePlayerButton(nr);});
                } else if (sot > 0) {//if positive number, we can go up!!
                    playerPosition[playerTurn] += latestRoll + sot;//button has new position
                    movePlayerButton(nr + sot);
                    //EventQueue.invokeLater(()->{movePlayerButton(nr);});
                } else {//god damn it we going down
                    playerPosition[playerTurn] += latestRoll - sot;//button has new position
                    movePlayerButton(nr - sot);
                    //EventQueue.invokeLater(()->{movePlayerButton(nr);});
                }
            changePlayerTurn(diceLabelText[1], diceLabelText[2]);
            roll.setEnabled(true);//next player can now roll
        }

    }
public void movePlayerButton(int spotNr){
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx=0;gbc.gridy=playerTurn-1;
        spotPanels[spotNr].add(playerButtons[playerTurn],gbc);//move players button to the new spot
        spotPanels[spotNr].setVisible(true);//set the panel to visible
    }

What I have tried:我尝试过的:

  • I have tried to call "frame.pack()" after each time a piece moves.每次移动后,我都尝试调用“frame.pack()”。 It seemed to work the first time it is called, but after that the frame begins to act wierd.(I at least tried something...)它似乎在第一次被调用时起作用,但在那之后框架开始变得奇怪。(我至少尝试了一些东西......)
  • I have tried EventQueue.InvokeLater and EventQueue.invokeAndWait.我试过 EventQueue.InvokeLater 和 EventQueue.invokeAndWait。 This most likely didn't work because I don't really know how to use it properly.这很可能不起作用,因为我真的不知道如何正确使用它。 java.awt.EventQueue.invokeLater explained java.awt.EventQueue.invokeLater解释

When changing components held within a container, one that properly uses a layout manager, you always should call revalidate() on the container or one of its parent containers, since this will tell the layout manager and the managers of any nested containers to re-layout their components.当更改容器中的组件时,正确使用布局管理器的组件,您应该始终在容器或其父容器之一上调用revalidate() ,因为这将告诉布局管理器和任何嵌套容器的管理器重新布局他们的组件。 You also often will want to call repaint() on the container to request a repainting of it and its children, mainly to clear any potentially left-over dirty pixels.您还经常希望在容器上调用repaint()以请求重新绘制它及其子项,主要是为了清除任何可能遗留的脏像素。 This latter is especially true when removing components from the container.当从容器中移除组件时,后者尤其如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM