JPanel 不会在 JLayeredPane 中动态显示



我正在用Java构建一个国际象棋程序。我无法在jPanel(即董事会(的顶部显示包含促销选项的jPanel。

棋盘位于 JLayeredPane 内,当棋子到达最后一个方格时,我在事件处理程序中实例化"促销选项"面板并重新验证。但是 jPanel 没有显示。

我已经尝试为jPanel对象设置适当的大小和位置,放置revalidate((,也使用了repaint((方法。我还明确地为 JLayeredPane 分配了一个 null 的布局。

堆栈溢出中已经与此相关的所有问题都已通过正确设置布局来解决,这似乎不是这里的问题......

// JLayeredPane created here...
class Game extends JFrame {
    Game() {
        super.setLayout(new TableLayout(new double[][]{{0.5, 475, 0.5}, {0.5, 475, 0.5}}));
        JLayeredPane layeredContainer = new JLayeredPane();
        layeredContainer.setLayout(null);
        Board board = new Board(layeredContainer);
        board.arrange();
        layeredContainer.add(board, 1);
        board.setSize(475, 475);
        super.add(layeredContainer, "1, 1");
        super.setSize(600, 600);
        super.setResizable(false);
        super.setDefaultCloseOperation(EXIT_ON_CLOSE);

        super.validate();
    }
}

此处创建的促销选项...

public class Board extends JPanel {
//skipping some irrelevant code...
private class PromotionOptions extends JPanel {
    private PromotionOptions(PieceColor color) {
        super.setSize(50,200);
        super.setBackground(Color.WHITE);
        super.setLayout(new GridLayout(4, 1));
        ImageIcon queenIcon = new ImageIcon(getClass().getResource("/pieceIcons/" + color.abbr + "Q.png"));
        ImageIcon rookIcon = new ImageIcon(getClass().getResource("/pieceIcons/" + color.abbr + "R.png"));
        ImageIcon bishopIcon = new ImageIcon(getClass().getResource("/pieceIcons/" + color.abbr + "B.png"));
        ImageIcon knightIcon = new ImageIcon(getClass().getResource("/pieceIcons/" + color.abbr + "N.png"));
        JLabel queenLabel = new JLabel(queenIcon);
        JLabel rookLabel = new JLabel(rookIcon);
        JLabel bishopLabel = new JLabel(bishopIcon);
        JLabel knightLabel = new JLabel(knightIcon);
        super.add(queenLabel);
        super.add(rookLabel);
        super.add(bishopLabel);
        super.add(knightLabel);
    }
}
// skipping some more irrelevant code...
private void executeMove(Tile clickedTile) {
// skip to where I create the PromotionOption object and add to layeredPane...
case PROMOTION:
                    moveMade.initialTile.removePiece();
                    JPanel promotionOptions = new PromotionOptions(colorToMove);
                    promotionOptions.setLocation(200,200);
                    layeredPaneContainer.add(promotionOptions, 2);
                    layeredPaneContainer.revalidate();
                    break;
// Some more code here
}
// End of Board class...
}

由于一些错误,每当棋子到达最终的瓷砖时,都不会显示任何内容,应用程序将继续保持原样。

经过数小时的沮丧调试,我终于以某种方式使其正常工作。我所做的是我添加了 SwingUtilities.invokeLater(( 方法,并在其中传递了这样的代码。

case PROMOTION:
                    moveMade.initialTile.removePiece();
                    SwingUtilities.invokeLater(() -> {
                        JPanel promotionOptions = new PromotionOptions(colorToMove.opposite);
                        promotionOptions.setLocation(clickedTile.getLocation());
                        layeredPaneContainer.add(promotionOptions, 2);
                        layeredPaneContainer.revalidate();
                    });
                    break;

最新更新