Java 添加到JPanel后,鼠标侦听器不工作

Java 添加到JPanel后,鼠标侦听器不工作,java,graphics,mouselistener,Java,Graphics,Mouselistener,我正在编写一个需要鼠标侦听器的游戏程序,它似乎没有添加到我的面板中,因为即使在我的鼠标方法中添加了sysout,也没有响应。以下是我的代码: public class Display extends JComponent implements MouseListener, MouseMotionListener{ //fields private GameBoardDisplay game; private JFrame frame; private JPanel boardPanel, co

我正在编写一个需要鼠标侦听器的游戏程序,它似乎没有添加到我的面板中,因为即使在我的鼠标方法中添加了sysout,也没有响应。以下是我的代码:

public class Display extends JComponent implements MouseListener, MouseMotionListener{

//fields
private GameBoardDisplay game;
private JFrame frame;
private JPanel boardPanel, controls;
private JButton nextGen, lastGen, clear, auto, genReturn;
private Timer time;
private JTextField genField;


//ctor that takes a GameOfLife object
public Display(GameOfLife game) {
    this.game = new GameBoardDisplay(game.getRows(), game.getCols());
    for (int row = 0; row < game.getRows(); row++) {
        for (int col = 0; col < game.getCols(); col++) {
            this.game.setState(row, col, game.getState(row, col));
        }
    }
}

//ctor that takes a 2D array
public Display(int[][] game){
    this.game = new GameBoardDisplay(game);
}

//ctor that takes the dimensions of the game
public Display(int rows, int cols){
    this.game = new GameBoardDisplay(rows,cols);
}

// sets up the Graphic user interface that displays the game
public void GUI() {
    boardPanel();
    controls();
    frame = new JFrame("Game Of Life Generation Number: " + game.getGenNum());
    frame.setVisible(true);
    frame.setSize(850, 900);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(game, BorderLayout.CENTER);
    frame.add(controls, BorderLayout.SOUTH);
    }

// sets up the game of life board display
public void boardPanel() {
    boardPanel = new JPanel();
    boardPanel.setLayout(new BorderLayout());
    boardPanel.add(game, BorderLayout.CENTER);
    boardPanel.setBackground(Color.WHITE);
    boardPanel.addMouseListener(this);
    boardPanel.addMouseMotionListener(this);
}   

//sets up the control panel
public void controls(){
    this.controls = new JPanel();
    //controls.add(lastGen);
    //lastGen();
    //controls.add(genField);
    //controls.add(genReturn);
    //genReturn();
    //controls.add(nextGen);
    nextGen();
    clear();
    auto();
    controls.add(clear);
    controls.add(auto);
}


public void genField(){
    final int FIELD_WIDTH = 10;
    genField = new JTextField(FIELD_WIDTH);

}


public static void main(String[] args) {
    Display tester = new Display(30,30);
    tester.GUI();

}

@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    int boxHeight = game.boxHeight();
    int boxWidth = game.boxWidth();
    int rowIndex = y / boxHeight;
    int columnIndex = x / boxWidth;
    if (game.getState(rowIndex, columnIndex) == 0) {
        game.setState(rowIndex, columnIndex, 1);
    } else {
        game.setState(rowIndex, columnIndex, 0);
    }
    game.repaint();
    System.out.println("Mouse pressed");
}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseDragged(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    int boxHeight = game.boxHeight();
    int boxWidth = game.boxWidth();
    int rowIndex = y / boxHeight;
    int columnIndex = x / boxWidth;
    game.setState(rowIndex, columnIndex, 1);
    game.repaint();
    System.out.println("Mouse dragged");
}

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

}
公共类显示扩展JComponent实现MouseListener、MouseMotionListener{
//田地
私人游戏板显示游戏;
私有JFrame;
私人JPanel控制面板;
私有JButton nextGen、lastGen、clear、auto、genReturn;
私人定时器时间;
私有JTextField-genField;
//获取GameOfLife对象的向量
公众展示(生活游戏){
this.game=新的GameBoardDisplay(game.getRows(),game.getCols());
对于(int row=0;row
}

这是我在另一个班上的绘画组件

public void paintComponent(Graphics g){
    int boxWidth = boxWidth();
    int boxHeight = boxHeight();
    for(int row=0; row<game.getRows(); row++){
        for(int col=0; col<game.getCols(); col++){
            g.setColor(Color.BLACK);
            g.drawRect(col*boxWidth, row*boxHeight, boxWidth, boxHeight);
            if(game.getState(row, col)==1){
                g.fillRect(col*boxWidth, row*boxHeight, boxWidth, boxHeight);
            }
        }
    }
公共组件(图形g){
int-boxWidth=boxWidth();
int-boxHeight=boxHeight();

对于(int row=0;row在使用
addMouseListener()
之后添加以下代码行:


这使窗口从其默认的不可聚焦形式变为可聚焦形式。

请看:Tenero,欢迎使用堆栈溢出!一般来说,请尝试只发布与您的问题相关的代码。请看:您是指boardPanel吗?因为BorderPanel不是Java API中的一个类。无论如何,我尝试了它,但失败了。@Tenero啊,好的,我们L我希望这可能行得通,但我想不行。我不知道还有什么能帮上忙的。你只需要等待其他答案。对此很抱歉。@Tenero但是,如果你编辑了你的帖子,只包含相关的信息/代码,我和其他人可能会更容易得到帮助。如果可能的话,用MosueListe写一个小例子ner有这个问题。试着将这个问题与MouseListener的使用隔离开来。对不起,请阅读我刚才说的。我在你回答后编辑了它。@Tenero我确实是指boardPanel,我的错。
setFocusable(true);