MouseListener不触发 [英] MouseListener not triggering

查看:103
本文介绍了MouseListener不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用这段代码来回处理鼠标事件.我有一个MainClass(实现MouseListener). UI具有JFrame,而JFrame具有basePanel. BasePanel具有GridPanel(调用Grid实现MouseListener). GridGridLayout上具有单独的JPanel.当我单击Grid时,它将触发Grid中的事件方法,但不会触发MainClass中的事件方法.它曾经可以更早地工作,但现在不再工作了.

I have been working on this code for long and going back and forth with mouse events. I have a MainClass (implements MouseListener). The UI has a JFrame which has a basePanel. The BasePanel has GridPanel (call Grid implements MouseListener). The Grid has individual JPanel on GridLayout. When I click on Grid it triggers the event methods in Grid but not in MainClass. It used to work earlier but not any more.

在界面方法中,我只输入了println来跟踪触发什么.

In the interface methods I have entered just println to tracks what's triggering.

MAINCLASS

MAINCLASS

public class PlayConnect implements MouseListener{

private JFrame mainFrame;
private JPanel basePanel,
buttonPanel,
messagePanel;

private Grid gridPanel;

private void startGame(){

    mainFrame = new JFrame("Connect-4");
    mainFrame.setSize(800, 700);    

    basePanel = new JPanel();
    basePanel.setName("basePanel");
    mainFrame.add(basePanel);

    gridPanel = new Grid();
    gridPanel.addMouseListener(this); //Added MouseListner
    gridPanel.setName("GridPanel");


    basePanel.add(gridPanel,BorderLayout.CENTER);

    messagePanel = new JPanel();
    //      messagePanel.addMouseListener(this);
    messagePanel.setName("messagePanel");

    messArea = new JTextArea();
    messArea.setEditable(false);
    messFont = new Font(Font.SERIF, Font.BOLD, 20);
    messArea.setFont(messFont);
    messArea.setText("Game On !");

    messagePanel.add(messArea);
    basePanel.add(messagePanel,BorderLayout.PAGE_END);


    buttonPanel = new JPanel();
    buttonPanel.setName("button Panel");
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));


    randButton = new JButton("Random Moves");
    buttonPanel.add(randButton);
    randButtonHandle = new RandomMoves();
    randButton.addActionListener(randButtonHandle);
    basePanel.add(buttonPanel,BorderLayout.LINE_END);


    mainFrame.setVisible(true);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

Grid

public class Grid extends JPanel implements MouseListener {

private Cell[][] gridUI = new Cell[6][7];
private static int[][] gridTrack = new int[6][7];
private static int player = 1;
private static Boolean gameOver = false;
public Boolean randPlayer;
private static ArrayList<Cell> cellArray = new ArrayList<Cell>();

public Grid(){
    //      setPreferredSize(new Dimension(600,700));;
    setLayout(new GridLayout(6, 7));
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 7; j++) {
            Cell tempCell = new Cell(i,j);
            tempCell.addMouseListener(this);
            gridUI[i][j] = tempCell;
            gridTrack[i][j] = 0;
            add(tempCell);

            int index = i*6 + j;
            cellArray.add(tempCell);

        }

    }
    addMouseListener(this);
}

推荐答案

这是Swing中事件处理的工作方式.从非常高的角度来看-生成事件时,将检查该坐标处的最高组件,以查看它是否会占用该事件.如果是这样,则事件被传递到该组件,并且处理停止;如果不是,则检查该位置的下一个最顶层组件,依此类推,直到到达顶层容器.一个事件永远不会传递给多个组件.

This is how event processing in Swing works. From a very high-level view - when an event is generated, the topmost component at that coordinate is checked to see if it will consume the event. If so, the event is delivered to that component and processing stops; if not, the next-topmost component at that location is checked, and so on until you reach the top-level container. An event will never be delivered to more than one component.

如果您确实需要顶级容器来获取所有事件,即使是在已注册侦听器的子级上,也可以使用AWTEventListener或GlassPane并自行处理事件的重新分发来完成,如前所述在此问题的答案中.

If you really need your top-level container to get all events, even on children with registered listeners, you can do so by using an AWTEventListener, or by using a GlassPane and handling re-dispatching the event yourself, both as described in answers to this question.

这篇关于MouseListener不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆