当可见性设置为false时,标签不会侦听事件

时间:2013-12-17 19:32:11

标签: java user-interface jlabel

首先抱歉我的英语不是我的母语。我想做一个迷宫游戏,因为我制作了一个数组,其中包含我的类“Cuadrado”的实例,它扩展了JLabel。每个实例代表迷宫路径中的一个点,因此路径中的块在哪里设置实例isVisible为true,因此它可以显示块的图像,如果特定路径为空,则将实例设置为false。游戏开始时一切都很好,但是当我点击它们时我想改变每个块的可见性,所以如果块是可见的并且我点击它我将可见设置为false但是每个实例在开始时都具有可见性为false不听点击事件,我不知道为什么

这是我的Cuadrado课程

public class Cuadrado extends JLabel
{
   public Cuadrado(int x, int y)
   {
       super(new ImageIcon("Biblioteca/Cuadrado.png"));
       this.setLocation(x, y);


        this.addMouseListener(new MouseOverListener());
    }

  class MouseOverListener extends MouseAdapter
  {
        @Override
        public void mousePressed(MouseEvent e)
        {
            System.out.println(isVisible());

            if(isVisible() == false)
            {
                System.out.println("Click");
                setVisible(true);
            }

            else
            {
                setVisible(false);
            }

    }

    @Override
    public void mouseDragged(MouseEvent e)
    {

        if(!isVisible())
            setVisible(true);

        else
            setVisible(false);
    }
}
}

这是我的Panel类

public class Stage extends JPanel 
{   

private Cuadrado[][] laberinto = new Cuadrado[7][7];

public Stage()
{

    this.setBackground(Color.BLACK);

    this.setLayout(new GridLayout(7,7));//fila x columna


    for(int fila = 0; fila < laberinto.length; fila++)
        for(int columna = 0; columna < laberinto[fila].length; columna++)
        {

            laberinto[fila][columna] = new Cuadrado(fila* 40, columna*40);

            //agregar los cuadrados al panel
            this.add(laberinto[fila][columna]);
        }

    try
    {

        File archivo = new File("Biblioteca/Laberinto inicial.txt");

        Scanner input = new Scanner(archivo);

        for(int fila = 0; fila < laberinto.length; fila++)
        {
            for(int columna = 0; columna < laberinto[fila].length; columna++)
            {   
                int i = input.nextInt();

                if(i == 0)
                    laberinto[fila][columna].setVisible(false);

                else if(i == 1)
                    laberinto[fila][columna].setVisible(true);
            }


            if(input.hasNext())
                input.nextLine();
        }

        input.close();
    }

    catch(IOException ex)
    {
        ex.printStackTrace();
    }

}

@Override
protected void paintComponent(Graphics g)
{
    super.paintComponent(g);

}

@Override
public Dimension getPreferredSize()
{
    return new Dimension(280, 280);
}
}

如果您知道为什么会发生这种情况,请告诉我我真的不知道问题出在哪里,如果您需要我更多解释来解释问题

0 个答案:

没有答案
相关问题