2

我有一个棋盘,有 64 个 JPanel 代表棋盘上的每个方格。这些片段使用放置在 JPanel 上的 JLabel 表示。我正在尝试从板上删除所有 JLabel。我很困惑为什么这不起作用:

private void removePieces()
{
    for(int i = 0; i < 64; i ++)
    {       
        Component c = chessBoard.getComponent(i);
        if(c instanceof JLabel)
        {
            Container parent = c.getParent();
            parent.remove((JLabel)c);
            parent.revalidate();
            parent.repaint();
        }
    }
}

棋盘是一个大的 JPanel,里面有 64 个 JPanel。经过一些调试后,似乎从未进入过 if 循环。我不明白如果其中一个组件是 JLabel,为什么它不会进入 if 循环?

4

3 回答 3

2

为什么要删除标签,而不是简单地将图标null或文本设置为""

EG 使用文本作为片段。

棋盘

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.border.LineBorder;

class ChessBoard2 {

    static ChessMoveMouseListener cmml = new ChessMoveMouseListener();
    /** Unicode strings for chess pieces & empty string for blank squares. */
    static String[][] pieces = {
        {"\u2654", "\u2655", "\u2656", "\u2657", "\u2658", "\u2659"},
        {"\u265A", "\u265B", "\u265C", "\u265D", "\u265E", "\u265F"},
        {""}
    };
    static int[] order = new int[]{2, 4, 3, 0, 1, 3, 4, 2};
    static int[] pawns = new int[]{5, 5, 5, 5, 5, 5, 5, 5};
    static int[] blank = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
    static int white = 0;
    static int black = 1;
    static int space = 2;

    public static JLabel getColoredLabel(String string, int color) {
        JLabel l = new JLabel(string);
        l.setFont(l.getFont().deriveFont(50f));
        Color c = (color % 2 == 0 ? Color.WHITE : Color.LIGHT_GRAY);
        l.setBackground(c);
        l.setOpaque(true);
        l.addMouseListener(cmml);

        return l;
    }

    public static void addRowToContainer(
            Container c,
            int[] order,
            int row,
            int count) {

        for (int ii : order) {
            c.add(getColoredLabel(pieces[row][ii], count++));
        }
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JPanel chessboard = new JPanel(new GridLayout(0, 8, 1, 1));
                chessboard.setBackground(Color.BLACK);
                chessboard.setBorder(new LineBorder(Color.BLACK));

                int count = 0;
                // black pieces..
                addRowToContainer(chessboard, order, black, count);
                addRowToContainer(chessboard, pawns, black, ++count);
                // middle squares..
                addRowToContainer(chessboard, blank, space, ++count);
                addRowToContainer(chessboard, blank, space, ++count);
                addRowToContainer(chessboard, blank, space, ++count);
                addRowToContainer(chessboard, blank, space, ++count);
                // white pieces..
                addRowToContainer(chessboard, pawns, white, ++count);
                addRowToContainer(chessboard, order, white, ++count);

                JOptionPane.showMessageDialog(null, chessboard,
                        "Click two squares to move from/to",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

class ChessMoveMouseListener extends MouseAdapter {

    String s = null;

    @Override
    public void mouseClicked(MouseEvent e) {
        JLabel l = (JLabel) e.getSource();
        if (s == null) {
            if (l.getText().trim().length() > 0) {
                s = l.getText();
                l.setText("");
            }
        } else {
            l.setText(s);
            s = null;
        }
    }
}
于 2013-09-19T12:31:10.830 回答
2

如果 JLabels 是 JLabels,您似乎试图从棋盘中删除 JPanel(这显然没有意义,这就是if代码永远不会触发的原因)。相反,您想删除chessBoard“组件”JLabel 组件。下面的例子。

private void removePieces() {
        for(int i = 0; i < 64; i ++) {   
            if(chessBoard.getComponent(i) instanceof JPanel) {
            JPanel c = (JPanel)chessBoard.getComponent(i);
            c.removeAll();
            c.revalidate();
            c.repaint();
            }
        }
    }

我正在使用removeAll(),因为我假设您的 JPanel 除了潜在的 JLabels 之外没有其他组件。

于 2013-09-19T14:24:53.150 回答
1

想想,当你在做的时候:

Component c = chessBoard.getComponent(i);

您将获得其中一个 JPanel,其中包含您的 JLabels。当然,它们不是 JLabel 的实例。因此,您需要从该 JPanel 中获取 JLabel,然后将其删除。

于 2013-09-19T06:51:13.413 回答