无法从JPanel中删除JLabel

时间:2013-09-19 06:42:43

标签: java swing user-interface jpanel jlabel

我有一个棋盘,里面有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();
        }
    }
}

棋盘是其中包含64个JPanel的大型JPanel。经过一些调试后,看起来永远不会输入if循环。我不明白为什么如果其中一个组件是JLabel,它不会进入if循环?

3 个答案:

答案 0 :(得分:2)

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

E.G。使用文字作为作品。

Chessboard

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;
        }
    }
}

答案 1 :(得分:2)

看起来你试图从你的棋盘中删除你的JPanels,如果它们是JLabel(这显然毫无意义,这就是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除了潜在的JLabel之外没有其他组件。

答案 2 :(得分:1)

当你在做的时候想想:

Component c = chessBoard.getComponent(i);

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