鼠标列表不在java中删除JPanel

时间:2016-10-16 17:48:11

标签: java swing jframe jpanel mouselistener

我有一个包含64个JPanel的JFrame。我有一个mouseListener应该删除被点击的JPanel。但是,单击JPanel时发生的唯一事情就是会出现一堆错误。

代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Checkers extends JPanel implements MouseListener {
    private final int ROW_COUNT = 8;
    private JFrame board = new JFrame();

    public Checkers() {
        GridLayout grid = new GridLayout(ROW_COUNT, ROW_COUNT);
        setLayout(grid);
        for (int i = 0; i < ROW_COUNT * ROW_COUNT; i++) {
            JPanel panel = new JPanel();
            int row = i / ROW_COUNT;
            int col = i % ROW_COUNT;
            String name = String.format("[%d, %d]", row, col);
            panel.setName(name);
            if ((row % 2) == (col % 2))
                panel.setBackground(Color.black);
            add(panel);
            if (panel.getBackground() == Color.black && row <= 2) {
                remove(panel);
                add(new Checker(Color.black, Color.red));
            }
            if (panel.getBackground() == Color.black && row >= 5) {
                remove(panel);
                add(new Checker(Color.black, Color.green));
            }
        }
    }

    public void startGame() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenHeight = (int) screenSize.getHeight();
        Insets inset = board.getInsets();
        int boardHeight = (screenHeight / 2) - inset.right - inset.left;
        int boardWidth = (screenHeight / 2) - inset.top - inset.bottom;
        board.setSize(boardHeight, boardWidth);
        board.setLocationRelativeTo(null);
        board.setVisible(true);
        board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        board.getContentPane().add(new Checkers());
        board.addMouseListener(this);
        addMouseListener(this);
    }

    public static void main(String[] args) {
        final Checkers obj = new Checkers();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                obj.startGame();
            }
        });
    }

    public void mouseClicked(MouseEvent e){ 
        JPanel panel = (JPanel) e.getComponent();
        remove(panel);
        System.out.println("mouseClicked has been called");
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }
}

class Checker extends JPanel {
    private Color circleColor;

    public Checker(Color c, Color cCircle) {
        setColor(c);
        setCircleColor(cCircle);
        repaint();
    }

    public void setColor(Color c) {
        setBackground(c);
    }

    public void setCircleColor(Color c) {
        circleColor = c;
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int width = getWidth();
        int height = getHeight();
        g.setColor(circleColor);
        int orgX = (width / 2) - ((width * 5) / 12);
        int orgY = (height / 2) - ((height * 5) / 12);
        int widthC = (width * 5) / 6;
        int heightC = (height * 5) / 6;
        g.fillOval(orgX, orgY, widthC, heightC);
    }
}

这些是错误:

Exception in thread "AWT-EventQueue-1" java.lang.ClassCastException: javax.swing.JFrame cannot be cast to javax.swing.JPanel
at Checkers.mouseClicked(Checkers.java:63)
at java.awt.Component.processMouseEvent(Component.java:6538)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Window.processEvent(Window.java:2029)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at org.GNOME.Accessibility.AtkWrapper$5.dispatchEvent(AtkWrapper.java:697)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:1)

  

ClassCastException:javax.swing.JFrame无法强制转换为javax.swing.JPanel

所以解决问题。您不能将MouseListener添加到JFrame并期望它是JPanel。

您需要:

  1. 在创建面板时在循环中添加MouseListener to each JPanel。然后,您可以将源转换为JPanel。

  2. 或者在MouseListener中,您需要编写在给定鼠标点搜索面板框架的代码。在这种情况下,您需要将源转换为JFrame。