这个问题是为了帮助我解决其他question问题。我有一个完全由JPanel覆盖的JLabels。我已经在面板上添加了MotionListener,但是由于标签妨碍了它,所以从未调用过它。如果删除标签,则会正确调用MotionnListener。

有什么办法可以使覆盖的面板看到运动事件?

public class ChangesAttempt extends JPanel {
    private static final long serialVersionUID = -8031881678612431401L;

    static JFrame frame;
    static JPanel grid;
    static JLabel[][] squares;
    static boolean[][] litSquares;
    static int size, boardSize;
    MouseEvent listener = new MouseEvent();
    MotionListener mListerner = new MotionListener();
    Color backGround = Color.BLUE;
    Color selected = Color.PINK;

    public ChangesAttempt(int size) {
        ChangesAttempt.size = size;
        squares = new JLabel[size][size];
        litSquares = new boolean[size][size];
        grid = new JPanel(new GridLayout(size, size));
        grid.addMouseMotionListener(mListerner);
        /*Coment out setBoard(); below to see the mousedDragged method actually being called
        since the JLabels will not be in the way.*/
        setBoard();

        frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.add(grid, BorderLayout.CENTER);
        frame.setTitle("ChangedLife");
        if (25 * size < 525)
            boardSize = 525;
        else
            boardSize = 25 * size;
        frame.setSize(boardSize, boardSize);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    class MouseEvent implements MouseListener {

        @Override
        public void mousePressed(java.awt.event.MouseEvent e) {
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    if (e.getSource() == squares[i][j]) {
                        if (litSquares[i][j] == false) {
                            squares[i][j].setBackground(selected);
                            litSquares[i][j] = true;
                        } else {
                            squares[i][j].setBackground(backGround);
                            litSquares[i][j] = false;
                        }
                    }
                }
            }
        }

        @Override
        public void mouseClicked(java.awt.event.MouseEvent e) {
        }

        @Override
        public void mouseEntered(java.awt.event.MouseEvent e) {
        }

        @Override
        public void mouseExited(java.awt.event.MouseEvent e) {
        }

        @Override
        public void mouseReleased(java.awt.event.MouseEvent e) {
        }
    }

    class MotionListener implements MouseMotionListener {
        @Override
        public void mouseDragged(java.awt.event.MouseEvent e) {
            Point p = e.getPoint();
            System.out.println("mouse Dragged to " + p);
        }

        @Override
        public void mouseMoved(java.awt.event.MouseEvent e) {
            // TODO Auto-generated method stub

        }
    }

    public void setBoard() {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                squares[i][j] = new JLabel();
                squares[i][j].addMouseListener(listener);
                squares[i][j].setOpaque(true);
                squares[i][j].setBackground(backGround);
                grid.add(squares[i][j]);
            }
        }
    }

    class MyLabel extends JLabel {
        private static final long serialVersionUID = -1414933339546989142L;

    }

    public static void main(String args[]) {
        new ChangesAttempt(20);
    }
}

最佳答案

没有实现此目的的“简便”方法,最好将所有鼠标侦听器移到面板上。

这将要求您在指定的点击点查找组件(位于父组件中),例如...

@Override
public void mousePressed(java.awt.event.MouseEvent e) {
    Component component = e.getComponent();
    Component source = component.getComponentAt(e.getPoint());
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (source == squares[i][j]) {
                if (litSquares[i][j] == false) {
                    squares[i][j].setBackground(selected);
                    litSquares[i][j] = true;
                } else {
                    squares[i][j].setBackground(backGround);
                    litSquares[i][j] = false;
                }
            }
        }
    }
}


如果您将MouseListener从标签中删除,而是将其添加到grid中,则可以使用此方法,并允许您使用MouseMotionListener

07-24 14:19