在for循环中添加侦听器,getsource为所有侦听器返回相同的值

时间:2015-03-23 08:33:33

标签: java swing loops mouse-listeners

我在循环中向JPanels添加鼠标侦听器,但e.getSource()始终返回最后一个JPanel而不是与循环中添加的侦听器对应的JPanel

public class MainFrame extends JFrame
{
    private PicPanel[] pics;
    private int locX = 100, locY = 50, Width = 800, Height = 600;

    public MainFrame(String[] filePaths)
    {
        pics = new PicPanel[filePaths.length];
        picStore(filePaths);

        mainConfig();
        updateGUI();
    }

    public void updateGUI()
    {
        repaint();
    }

    private void mainConfig()
    {
        this.setBounds(locX, locY, Width, Height);
        this.setLayout(new CardLayout());
        this.setBackground(Color.DARK_GRAY);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    private void picStore(String[] filePaths)
    {
        ML ml = new ML();

        for (int i = 0; i < filePaths.length; i++)
        {
            pics[i] = new PicPanel(filePaths[i], randomizeCords(), this);
            pics[i].addMouseListener(ml);
            pics[i].addMouseMotionListener(ml);
            this.add(pics[i]);
        }
    }
class ML implements MouseInputListener
    {
        @Override
        public void mouseClicked(MouseEvent e)
        {
            if (e.getSource() instanceof PicPanel)
            {
                for (int i = 0; i < pics.length; i++)
                {
                    if (e.getSource().equals(pics[i]))
                        pics[i].setShowBack(!pics[i].getShowBack());
                    updateGUI();
                }
            }
        }

public class PicPanel extends JPanel implements MouseMotionListener
{
    private ImageIcon backSide =  new ImageIcon("C:/bilder/back.jpg");
    private ImageIcon pic;
    private Point loc;
    private boolean showBack = false;
    MainFrame main;

    public PicPanel(String filePath, Point start, MainFrame main)
    {
        pic  = new ImageIcon(filePath);
        this.loc = start;
        this.main = main;
        this.addMouseMotionListener(this);
    }
    @Override
    public void paintComponent(Graphics g)
    {
        //super.paintComponent(g);
        Image image;

        if (showBack)
        {
            image = backSide.getImage();

        }
        else
        {
            image = pic.getImage();
        }
        double scaleFactor = (double)image.getWidth(null)/(double)image.getHeight(null);

        g.drawImage(image, loc.x, loc.y, (int)(100 * scaleFactor), 100, null);
    }

    @Override
    public void mouseDragged(MouseEvent e)
    {
        //Checks so the mouse is inside the component
        if ((e.getX() > this.loc.x && e.getX() < (this.loc.x) + this.getWidth()) && (e.getY() > this.loc.y && e.getY() < (this.loc.y + this.getHeight())))
        {
            this.setLoc(new Point(e.getX() - 50, e.getY() - 50));
            main.repaint();
        }
    }

编辑: 即使我在PicPanel中创建了监听器,它仍然只影响最后添加的监听器 编辑:添加更多代码以澄清

0 个答案:

没有答案