简体   繁体   English

在JPanel中拖动JComponent

[英]Drag JComponent within JPanel

I have a subclass of JPanel which contains an array of JComponent objects. 我有一个JPanel子类,其中包含一个JComponent对象数组。 I then use the paint(Graphics g) method to position the JComponent objects next to each other in the panel. 然后,我使用paint(Graphics g)方法将JComponent对象在面板中彼此相邻放置。 All these JComponent Objects implement MouseMostionListener and I initialise the listener using addMouseMotionListener(this); 所有这些JComponent对象都实现MouseMostionListener,并且我使用addMouseMotionListener(this);初始化侦听器addMouseMotionListener(this); , I also have the methods mouseMoved(MouseEvent m) and mouseDragged(MouseEvent m) . ,我还有方法mouseMoved(MouseEvent m)mouseDragged(MouseEvent m) All the components are being drawn correctly but the mouseMoved(MouseEvent m) and mouseDragged(MouseEvent m) are never called. 所有组件均已正确绘制,但从不调用mouseMoved(MouseEvent m)mouseDragged(MouseEvent m) Any ideas why? 有什么想法吗?

Here is my code: JPanel Subclass 这是我的代码:JPanel子类

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: JComponent子类:

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");
    }
}

1)Add components like you do isn't proper way. 1)添加组件是不正确的。

2)Instead of drawing Image use JLabel with icon, you get a lot of advantages with it from 'box'. 2)除了使用JLabel和图标来绘制图像,您还可以从“盒子”中获得很多好处。

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");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM