JLabel鼠标事件一键运行两次

时间:2018-12-12 12:19:56

标签: java swing awt mouseevent jlabel

它在最新版本中没有,但是在最新版本中,尽管我只单击了一次,但它可以工作两次。

public void mouseClicked(MouseEvent e) {
    ((MouseListener)a).mouseClicked(e);
    ((MouseListener)b).mouseClicked(e);
}

代码来自库类AWTEventMulticaster.class。似乎当我单击一次时,侦听器会两次唤醒。但是奇怪的是,一个事件听众可以工作两次。不仅如此,所有鼠标事件都已更改为工作两次,所以我的所有代码都毁了。有什么解决办法吗?

更多信息在这里。首先,是我的鼠标监听程序的代码。

Label1.addMouseListener(new MouseAdapter()//Begin
    {
        public void mousePressed(MouseEvent e)
        {
            if(e.getButton()==MouseEvent.BUTTON1)
            {
                Label1.pointerInfo = MouseInfo.getPointerInfo();                    
                Label1.setIsPressed(true);
            }
        }
        public void mouseReleased(MouseEvent e)
        {
            if(e.getButton()==MouseEvent.BUTTON1)
            {
                if(Label1.getLocation().y<555 && Label1.getIsCard()==true)
                {                       
                    int cn = Label1.getCardNumX();
                    Label1.setCardNumY(getY(cn));                       
                    Label1.setText(null);
                    String s = "/image/\uBE14\uB85D "+Integer.toString(cn)+"-"+Integer.toString(Label1.getCardNumY())+".png";
                    ImageIcon im = new ImageIcon(Game.class.getResource(s));
                    Label1.setImage(im.getImage());
                    Label1.setIcon(setIconScale(im, Label1.getCardNumX(), Label1.getCardNumY(), 'L'));
                    Label1.setIsCard(false);
                }
                else if(Label1.getIsCard()==true)
                {
                    Label1.setBounds(70, 600, 150, 150);
                }
                else if(Label1.getLocation().y>675&&Used[0]!=1)
                {
                    Image I = Label1.getImage();
                    ImageIcon im;
                    if(Label1.getIsFlipped()==true)
                    {
                        BufferedImage i = convertToBufferedImage(I);
                        if(Label1.getRot()%2==1)
                            i= verticalFlip(i);
                        else
                            i = horizontalFlip(i);
                        im = new ImageIcon(i);
                    }
                    else
                        im = new ImageIcon(I);                  
                    im = setIconScale(im, Label1.getCardNumX(), Label1.getCardNumY(), 'L');
                    RotatedIcon i = new RotatedIcon(im, (0 - (Label1.getRot()*(90.0))));
                    Label1.setIcon(i);
                    Label1.setBounds(70, 600, 150, 150);
                }
                else if(Used[0]!=1)
                {
                    Icon r = Label1.getIcon();
                    if(Label1.getIsCard() == false && r!= null && BoardLavel.CheckBlock(MouseInfo.getPointerInfo(), Label1.a, Label1.BlockX, Label1.BlockY))                        
                    {
                        Used[0] = 1;
                        Deck[0] = 0;
                        CheckGreen();
                        Label1.setIcon(null);
                        score = score + 100 * BoardLavel.CheckErase();
                        Score.setText("Score = " + Integer.toString(score));
                    }
                    else
                    {
                        Image I = Label1.getImage();
                        ImageIcon im;
                        if(Label1.getIsFlipped()==true)
                        {
                            BufferedImage i = convertToBufferedImage(I);
                            if(Label1.getRot()%2==1)
                                i= verticalFlip(i);
                            else
                                i = horizontalFlip(i);
                            im = new ImageIcon(i);
                        }
                        else
                            im = new ImageIcon(I);                  
                        im = setIconScale(im, Label1.getCardNumX(), Label1.getCardNumY(), 'L');
                        RotatedIcon i = new RotatedIcon(im, (0 - (Label1.getRot()*(90.0))));
                        Label1.setIcon(i);
                    }
                }
                Label1.setBounds(70, 600, 150, 150);
                Label1.setIsPressed(false);
            }
        }
        public void mouseClicked(MouseEvent e)
        {
            if(Label1.isCard==false)
            {if(e.getButton()==MouseEvent.BUTTON3){
                Label1.changeIsFlipped();
                }
            else if(e.getButton()==MouseEvent.BUTTON1){
            if(Label1.getIsFlipped()==true)
            {
                Label1.setIsRotated(-1);
            }
            else
                Label1.setIsRotated(1);
            }
            Image image = Label1.getImage();
            ImageIcon ima;
            if(Label1.getIsFlipped()==true)
            {
                BufferedImage im = convertToBufferedImage(image);
                if(Label1.getRot()%2==1)
                    im = verticalFlip(im);
                else
                    im = horizontalFlip(im);
                ima = new ImageIcon(im);
            }
            else
                ima = new ImageIcon(image);
            ima = setIconScale(ima, Label1.getCardNumX(), Label1.getCardNumY(), 'L');
            RotatedIcon i = new RotatedIcon(ima, (0 - (Label1.getRot()*(90.0))));
            Label1.setIcon(i);
            }
        }
    });

第二,Listner上使用的功能。

protected void CheckGreen() {
    for(int i=0;i<7;i++)
        for(int j=0;j<7;j++)
            if(BoardLavel.Board[i][j]==1)
            {
                switch(i)
                {
                case 0:
                {
                    switch(j)
                    {
                    case 0:
                        BoardLabel6_0.setIcon(GreenBoardimg);
                        break;
                    case 1:
                        BoardLabel6_1.setIcon(GreenBoardimg);
                        break;
                    case 2:
                        BoardLabel6_2.setIcon(GreenBoardimg);
                        break;
                    case 3:
                        BoardLabel6_3.setIcon(GreenBoardimg);
                        break;
                    case 4:
                        BoardLabel6_4.setIcon(GreenBoardimg);
                        break;
                    case 5:
                        BoardLabel6_5.setIcon(GreenBoardimg);
                        break;
                    case 6:
                        BoardLabel6_6.setIcon(GreenBoardimg);
                        break;
                    }
                }
                    break;
                case 1:
                {
                    switch(j)
                    {
                    case 0:
                        BoardLabel5_0.setIcon(GreenBoardimg);
                        break;
                    case 1:
                        BoardLabel5_1.setIcon(GreenBoardimg);
                        break;
                    case 2:
                        BoardLabel5_2.setIcon(GreenBoardimg);
                        break;
                    case 3:
                        BoardLabel5_3.setIcon(GreenBoardimg);
                        break;
                    case 4:
                        BoardLabel5_4.setIcon(GreenBoardimg);
                        break;
                    case 5:
                        BoardLabel5_5.setIcon(GreenBoardimg);
                        break;
                    case 6:
                        BoardLabel5_6.setIcon(GreenBoardimg);
                        break;
                    }
                }
                    break;
                case 2:
                {
                    switch(j)
                    {
                    case 0:
                        BoardLabel4_0.setIcon(GreenBoardimg);
                        break;
                    case 1:
                        BoardLabel4_1.setIcon(GreenBoardimg);
                        break;
                    case 2:
                        BoardLabel4_2.setIcon(GreenBoardimg);
                        break;
                    case 3:
                        BoardLabel4_3.setIcon(GreenBoardimg);
                        break;
                    case 4:
                        BoardLabel4_4.setIcon(GreenBoardimg);
                        break;
                    case 5:
                        BoardLabel4_5.setIcon(GreenBoardimg);
                        break;
                    case 6:
                        BoardLabel4_6.setIcon(GreenBoardimg);
                        break;
                    }
                }
                    break;
                case 3:
                {
                    switch(j)
                    {
                    case 0:
                        BoardLabel3_0.setIcon(GreenBoardimg);
                        break;
                    case 1:
                        BoardLabel3_1.setIcon(GreenBoardimg);
                        break;
                    case 2:
                        BoardLabel3_2.setIcon(GreenBoardimg);
                        break;
                    case 3:
                        BoardLabel3_3.setIcon(GreenBoardimg);
                        break;
                    case 4:
                        BoardLabel3_4.setIcon(GreenBoardimg);
                        break;
                    case 5:
                        BoardLabel3_5.setIcon(GreenBoardimg);
                        break;
                    case 6:
                        BoardLabel3_6.setIcon(GreenBoardimg);
                        break;
                    }
                }
                    break;
                case 4:
                {
                    switch(j)
                    {
                    case 0:
                        BoardLabel2_0.setIcon(GreenBoardimg);
                        break;
                    case 1:
                        BoardLabel2_1.setIcon(GreenBoardimg);
                        break;
                    case 2:
                        BoardLabel2_2.setIcon(GreenBoardimg);
                        break;
                    case 3:
                        BoardLabel2_3.setIcon(GreenBoardimg);
                        break;
                    case 4:
                        BoardLabel2_4.setIcon(GreenBoardimg);
                        break;
                    case 5:
                        BoardLabel2_5.setIcon(GreenBoardimg);
                        break;
                    case 6:
                        BoardLabel2_6.setIcon(GreenBoardimg);
                        break;
                    }
                }
                    break;
                case 5:
                {
                    switch(j)
                    {
                    case 0:
                        BoardLabel1_0.setIcon(GreenBoardimg);
                        break;
                    case 1:
                        BoardLabel1_1.setIcon(GreenBoardimg);
                        break;
                    case 2:
                        BoardLabel1_2.setIcon(GreenBoardimg);
                        break;
                    case 3:
                        BoardLabel1_3.setIcon(GreenBoardimg);
                        break;
                    case 4:
                        BoardLabel1_4.setIcon(GreenBoardimg);
                        break;
                    case 5:
                        BoardLabel1_5.setIcon(GreenBoardimg);
                        break;
                    case 6:
                        BoardLabel1_6.setIcon(GreenBoardimg);
                        break;
                    }
                }
                    break;
                case 6:
                {
                    switch(j)
                    {
                    case 0:
                        BoardLabel0_0.setIcon(GreenBoardimg);
                        break;
                    case 1:
                        BoardLabel0_1.setIcon(GreenBoardimg);
                        break;
                    case 2:
                        BoardLabel0_2.setIcon(GreenBoardimg);
                        break;
                    case 3:
                        BoardLabel0_3.setIcon(GreenBoardimg);
                        break;
                    case 4:
                        BoardLabel0_4.setIcon(GreenBoardimg);
                        break;
                    case 5:
                        BoardLabel0_5.setIcon(GreenBoardimg);
                        break;
                    case 6:
                        BoardLabel0_6.setIcon(GreenBoardimg);
                        break;
                    }
                }
                    break;
            }
        }
}
protected RotatedIcon rotate(Icon icon)
{
    RotatedIcon rot = new RotatedIcon(icon, -90);
    return rot;
}
protected Image RescaledBlock(ImageIcon icon, int x, int y)
{
    Image origin = icon.getImage();
    Image chan= origin.getScaledInstance(x, y, Image.SCALE_SMOOTH );
    return chan;
}
public static BufferedImage horizontalFlip(BufferedImage img) {
    int w = img.getWidth();
    int h = img.getHeight();
    BufferedImage flippedImage = new BufferedImage(w, h, img.getType());
    Graphics2D g = flippedImage.createGraphics();
    g.drawImage(img, 0, 0, w, h, w, 0, 0, h, null);
    g.dispose();
    return flippedImage;
}
public static BufferedImage convertToBufferedImage(Image image)
{
    BufferedImage newImage = new BufferedImage(
        image.getWidth(null), image.getHeight(null),
        BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = newImage.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return newImage;
}
public static BufferedImage verticalFlip(BufferedImage img) {
    int w = img.getWidth();
    int h = img.getHeight();
    BufferedImage flippedImage = new BufferedImage(w, h, img.getColorModel().getTransparency());
    Graphics2D g = flippedImage.createGraphics();
    g.drawImage(img, 0, 0, w, h, 0, h, w, 0, null);
    g.dispose();
    return flippedImage;
}
public ImageIcon setIconScale(ImageIcon i, int x, int y, char ch)
{
    ImageIcon im = i;
    if(x==1)
    {
        if(ch=='s')
            im = new ImageIcon(RescaledBlock(im, 20, 20));
        else
            im = new ImageIcon(RescaledBlock(im, 50, 50));
    }
    if(x==2)
    {
        if(y==0)
        {
            if(ch=='s')
                im = new ImageIcon(RescaledBlock(im, 40, 20));
            else
                im = new ImageIcon(RescaledBlock(im, 100, 50));
        }
        else
        {
            if(ch=='s')
                im = new ImageIcon(RescaledBlock(im, 40, 40));
            else
                im = new ImageIcon(RescaledBlock(im, 100, 100));
        }
    }
    if(x==3)
    {
        if(y==0)
        {
            if(ch=='s')
                im = new ImageIcon(RescaledBlock(im, 60, 20));
            else
                im = new ImageIcon(RescaledBlock(im, 150, 50));
        }
        else if(y==1)
        {
            if(ch=='s')
                im = new ImageIcon(RescaledBlock(im, 40, 40));
            else
                im = new ImageIcon(RescaledBlock(im, 100, 100));
        }
        else if(y==2)
        {
            if(ch=='s')
                im = new ImageIcon(RescaledBlock(im, 60, 60));
            else
                im = new ImageIcon(RescaledBlock(im, 150, 150));
        }
        else
        {
            if(ch=='s')
                im = new ImageIcon(RescaledBlock(im, 60, 40));
            else
                im = new ImageIcon(RescaledBlock(im, 150, 100));
        }
    }
    if(x==4)
    {
        if(y==0)
        {
            if(ch=='s')
                im = new ImageIcon(RescaledBlock(im, 40, 40));
            else
                im = new ImageIcon(RescaledBlock(im, 100, 100));
        }
        else if(y==1||y==2||y==3||y==6)
        {
            if(ch=='s')
                im = new ImageIcon(RescaledBlock(im, 60, 40));
            else
                im = new ImageIcon(RescaledBlock(im, 150, 100));
        }
        else
        {
            if(ch=='s')
                im = new ImageIcon(RescaledBlock(im, 60, 60));
            else
                im = new ImageIcon(RescaledBlock(im, 150, 150));
        }
    }
    if(x==5)
    {
        if(y==3)
        {
            if(ch=='s')
                im = new ImageIcon(RescaledBlock(im, 60, 40));
            else
                im = new ImageIcon(RescaledBlock(im, 150, 100));
        }
        else if(y==7)
        {
            if(ch=='s')
                im = new ImageIcon(RescaledBlock(im, 40, 60));
            else
                im = new ImageIcon(RescaledBlock(im, 100, 150));
        }
        else
        {
            if(ch=='s')
                im = new ImageIcon(RescaledBlock(im, 60, 60));
            else
                im = new ImageIcon(RescaledBlock(im, 150, 150));
        }
    }
    return im;
}
public int getY(int cn)
{
    Random ra = new Random();
    int a = 0;
    switch(cn)
    {
    case 1:
        a =  0;
        break;
    case 2:
        a = ra.nextInt(2);
        break;
    case 3:
        a = ra.nextInt(5);
        break;
    case 4:
        a = ra.nextInt(12);
        break;
    case 5:
        a = ra.nextInt(17);
        break;
    }
    return a;
}

第三,在此处找到RotatedIcon类;

https://tips4java.wordpress.com/2009/04/06/rotated-icon/

如果您需要更多信息,请在此处评论。

0 个答案:

没有答案