Java MouseListener MouseClicked 只能工作一次

标签 java swing mouselistener

我使用 JPanel 为 Game Of Life 制作 GUI,我的类扩展了 JFrame 并实现了 MouseListener,但是当我单击 JLabel 时,MouseListener 只工作一次。

知道该怎么做吗?这在我的 GameOfLife 类中的代码中不是问题,因为我也尝试打印一些字符串,但它只能工作一次。

谢谢! 这是类(class):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.*;
import javax.swing.border.Border;


public class GameOfLifeWindow extends JFrame implements ActionListener, MouseListener
{
    private static final int DEF_ROWS = 16;
    private static final int DEF_COLS = 16;
    private JLabel[][] cells;
    GameOfLife gol;
    private JPanel panClear;
    private JPanel panCenter;
    private JPanel panNextGen;
    private JButton nextGen;
    private JButton clear;
    private JMenuBar menuBar;
    private JMenu file;
    private JMenuItem newGame;
    private JMenuItem loadFile;

    public GameOfLifeWindow()
    {
        super("Game Of Life");
        gol = new GameOfLife(DEF_ROWS, DEF_COLS);

        //sets layout and dimension
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().setPreferredSize(new Dimension(300,400));

        //Sets the panels
        panClear = new JPanel(new FlowLayout());
        panCenter = new JPanel(new GridLayout(gol.getRows(), gol.getCols()));
        panNextGen = new JPanel(new FlowLayout());

        //Adds the panel to the JFrame
        this.getContentPane().add(panClear, BorderLayout.NORTH);
        this.getContentPane().add(panCenter, BorderLayout.CENTER);
        this.getContentPane().add(panNextGen, BorderLayout.SOUTH);

        //Sets the next generation button
        this.nextGen = new JButton("Next Generation");
        panNextGen.add(this.nextGen);
        this.nextGen.addActionListener(this);
        //Sets the clear button
        ImageIcon btnIcon = new ImageIcon(getClass().getResource("images/clear.png"));
        Image tmpImg = btnIcon.getImage();
        BufferedImage bi = new BufferedImage(30, 30, BufferedImage.TYPE_INT_ARGB);
        Graphics g = bi.createGraphics();
        g.drawImage(tmpImg, 0, 0, 30, 30, null);
        this.clear = new JButton(new ImageIcon(bi));
        this.clear.addActionListener(this);
        panClear.add(clear);

        //Set the GridLayout
        this.updateGeneration();        

        //Set the mouse listener to each cell
        for(int i = 0 ; i < this.cells.length ; i++)
            for(int j = 0 ; j < this.cells[i].length ; j++)
                this.cells[i][j].addMouseListener(this);

        //Sets MenuBar
        this.menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);

        //Sets the File menu
        this.file = new JMenu("File");
        menuBar.add(file);
        //Sets the New Game in the file
        this.newGame = new JMenuItem("New Game");
        this.newGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, InputEvent.CTRL_MASK));
        this.newGame.addActionListener(this);
        file.add(this.newGame);

        //Sets the Load File in the file
        this.loadFile = new JMenuItem("Load File");
        this.loadFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2, InputEvent.CTRL_MASK));
        this.loadFile.addActionListener(this);
        file.add(this.loadFile);
    }

    public void updateGeneration()
    //updates the JLabels according to the Game Of Life Screen
    {
        Border border = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
        this.getContentPane().remove(this.panCenter);
        this.panCenter = new JPanel(new GridLayout(gol.getRows(), gol.getCols()));
        this.cells = new JLabel[gol.getRows()][gol.getCols()];
        this.panCenter.removeAll();
        for(int i = 0 ; i < cells.length ; i++)
            for(int j = 0 ; j < cells[i].length ; j++)
            {
                this.cells[i][j] = new JLabel();
                this.cells[i][j] .setOpaque(true); // make the color visible
                this.cells[i][j].setBorder(border);// sets borders
                if(gol.isAlive(i, j))
                    this.cells[i][j].setBackground(Color.BLACK);
                else
                    this.cells[i][j].setBackground(Color.white);
                panCenter.add(this.cells[i][j]);
            }

        this.getContentPane().add(this.panCenter);
        this.setVisible(true);      
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == this.nextGen)
        {
            this.gol.nexGeneration();
            this.updateGeneration();
        }
        else if(e.getSource() == this.clear)
        {
                this.gol = new GameOfLife(DEF_ROWS, DEF_COLS);
                this.updateGeneration();
        }
        else if(e.getSource() == this.newGame)
        {
            this.gol = new GameOfLife(DEF_ROWS, DEF_COLS);
            this.updateGeneration();
        }
        else if(e.getSource() == this.loadFile)
        {
            String fileName = JOptionPane.showInputDialog("Please Enter A File Name");
            this.gol = new GameOfLife(fileName);
            System.out.println(gol.toString());
            this.updateGeneration();
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) 
    {
        boolean found = false;
        for(int i = 0 ; i < cells.length && !found ; i++)
            for(int j = 0 ; j < cells[i].length && !found; j++)
                if(e.getSource() == this.cells[i][j])
                {
                    this.gol.setForGUI(i, j);
                    found = true;
                }
        this.updateGeneration();
        System.out.println("asdsad");
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }
}

最佳答案

在构造函数中,您将鼠标监听器分配给 JLabel 多维数组中的所有单元格。但是,然后在 updateGeneration() 中创建另一个 JLabel[][] 数组,但不要再次将鼠标监听器分配给单元格。

要修复此问题,您需要在 updateGeneration() 中再次向所有单元格添加鼠标监听器。或者,无需创建新的 JLabel[][],只需更新现有数组中 JLabel 的状态。

关于Java MouseListener MouseClicked 只能工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26238982/

相关文章:

java - 如何在 Java 中为临时目录配置 root

java - 在 JProgressBar 上显示自定义进度字符串

java - 覆盖鼠标的速率设置。创建我自己的鼠标速率算法

java用鼠标移动组件

java - 如何创建动态 JList?

java - 只需单击面板的一部分即可打开新面板

java - 始终使用 jhipster 将日期保存到数据库中的今天日期

java - 凭据不能用于 NTLM 身份验证

java - IBM Bluemix : How the deploy an application on Bluemix?

java - swing jtable默认显示多行