Question

I have a subclass of JPanel which contains an array of JComponent objects. I then use the paint(Graphics g) method to position the JComponent objects next to each other in the panel. All these JComponent Objects implement MouseMostionListener and I initialise the listener using addMouseMotionListener(this);, I also have the methods mouseMoved(MouseEvent m) and mouseDragged(MouseEvent m). All the components are being drawn correctly but the mouseMoved(MouseEvent m) and mouseDragged(MouseEvent m)are never called. Any ideas why?

Here is my code: JPanel Subclass

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Image;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.util.ArrayList;

public class ExamplePanel extends JPanel
{
    ArrayList<ExampleComponent> components;

    public ExamplePanel()
    {
        components = new ArrayList<ExampleComponent>();
    }

    public void paint(Graphics g)
    {
        for(ExampleComponent c : components)
            g.drawImage(c.getImage(), 0, 30, 50, 75, null);
    }

    public void addComponent(ExampleComponent j)
    {
        components.add(j);
        repaint();
    }

    public static void main(String[] args) 
    {
        JFrame app = new JFrame("Staff Prototype");
        app.setSize(700,200);
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.setResizable(false);

        ExamplePanel s = new ExamplePanel();
        app.getContentPane().add(s);
        s.addComponent(new ExampleComponent());
        app.setVisible(true);
    }
}

JComponent Subclass:

import java.awt.Image;
import javax.swing.JComponent;
import javax.swing.ImageIcon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class ExampleComponent extends JComponent implements MouseMotionListener
{
    Image image;

    public ExampleComponent()
    {
        ImageIcon icon = new ImageIcon("image.png");
        image = icon.getImage();
        addMouseMotionListener(this);
    }

    public Image getImage()
    {
        return image;
    }

    public void mouseMoved(MouseEvent m) 
    {
        System.out.println("Mouse Moved");
    }

    public void mouseDragged(MouseEvent m)
    {
        System.out.println("Mouse Dragged");
    }
}
Était-ce utile?

La solution

1)Add components like you do isn't proper way.

2)Instead of drawing Image use JLabel with icon, you get a lot of advantages with it from 'box'.

I have fixed your code, examine that:

import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Example extends JPanel {
    ArrayList<ExampleComponent> components;

    public Example() {
        components = new ArrayList<ExampleComponent>();
    }


    public void addComponent(ExampleComponent j) {
        components.add(j);
        add(j);
    }

    public static void main(String[] args) {
        JFrame app = new JFrame("Staff Prototype");
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Example s = new Example();
        s.setLayout(null);
        app.getContentPane().add(s);
        s.addComponent(s.new ExampleComponent(new Rectangle(0,0,25,25)));
        s.addComponent(s.new ExampleComponent(new Rectangle(45,45,25,25)));
        app.pack();
        app.setVisible(true);
    }

    class ExampleComponent extends JPanel implements MouseMotionListener {

        public ExampleComponent(Rectangle bounds) {
            URL resource = getClass().getResource("3_disc.png");
            ImageIcon icon = new ImageIcon(resource);
            add(new JLabel(icon));
            addMouseMotionListener(this);
            setBounds(bounds);
        }

        public void mouseMoved(MouseEvent m) {
            System.out.println("Mouse Moved");
        }

        public void mouseDragged(MouseEvent m) {
            System.out.println("Mouse Dragged");
        }
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top