Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何设置鼠标侦听器的优先级_Java_Swing_Drag And Drop_Mouseevent_Jlist - Fatal编程技术网

Java 如何设置鼠标侦听器的优先级

Java 如何设置鼠标侦听器的优先级,java,swing,drag-and-drop,mouseevent,jlist,Java,Swing,Drag And Drop,Mouseevent,Jlist,我有一个带有列表和按钮的面板。列表使用mouseClick()设置MouseAdapter。我用mousePressed()和mousererelease()和MouseMotionAdapter向面板添加了MouseAdapter 只有在单击面板时,拖放才起作用 如何使拖动工作,即使我点击了列表 简单示例: public class DragTest extends JFrame{ private boolean drag; private Point btnCoord; private Po

我有一个带有列表和按钮的面板。列表使用
mouseClick()
设置
MouseAdapter
。我用
mousePressed()
mousererelease()
MouseMotionAdapter
向面板添加了
MouseAdapter

只有在单击面板时,拖放才起作用

如何使拖动工作,即使我点击了列表

简单示例:

public class DragTest extends JFrame{
private boolean drag;
private Point btnCoord;
private Point startPoint;

public DragTest(){
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(500,500);
    setLayout(null);
    final JPanel panel = new JPanel();
    final JButton button = new JButton();
    button.setText("Button");
    button.setSize(30,60);
    button.setLocation(50, 50);
    panel.setLayout(null);
    setContentPane(panel);
    panel.addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            if (drag){
                panel.setLocation(btnCoord.x-(startPoint.x-e.getX()),btnCoord.y-(startPoint.y-e.getY()));
            }
        }
    });
    panel.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            drag = true;
            startPoint = e.getPoint();
            btnCoord = panel.getLocation();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            drag = false;
        }
    });
    getContentPane().add(button);
}

public static void main(String[] args) {
    JFrame frame = new DragTest();
    frame.setVisible(true);
}
}

如果拖动面板,所有工作正常,如果启动拖动按钮,则按钮拦截事件。

以上代码有许多错误

  • 不要使用
    null
    /
    Absolute
    LayoutManager
  • 不要在组件和
    JFrame
    上调用
    setSize
    (如果实现了正确的布局管理器,您只需添加组件并在
    JFrame
    上调用
    pack()
  • 在EDT上创建和操作Swing组件。有关详细信息,请阅读
  • 从可见容器中添加/删除组件后,应在容器上调用
    revalidate()
    repaint()
我一直在开发自己的小
ComponentDrag
API,它允许我们在
JComponent
或多个
JComponent
s上拖动Swing组件。但这绝不是没有错误的(特别是多组件部分今天刚刚完成,所以你永远不知道)

您将创建
JButton
并添加到
JPanel

    JButton button = new JButton("Text");

    JPanel panel1 = new JPanel() {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    };
    panel1.add(button);
然后通过传递容器的参数,即
JFrame
/
JDialog
JWindow
来创建
ComponentDrag
类的实例,并注册可拖动组件并调用
setName(“可拖放”)
在您的
JPanel上
为了允许正确地进行拖放操作,尤其是在多个jcomponent等之间,您只能对希望能够拖放Swing组件的
JPanel等执行此操作:

    JFrame frame=..;//has access to setGlassPane same applies for JDialog and JWindow
    frame.add(panel1);

    ComponentDrag cd=new ComponentDrag(frame);
    cd.registerComponent(button);
    panel1.setName("Droppable");//needed for the dragging and dropping to work correctly especailly over multiple components
还有你的好去处:)

以下是用于分析目的的两个完整示例:

1) 显示移动
JButton
s的逻辑,并使用
ComponentDrag
并覆盖其
componentdrop
方法相应地重新定位:

拖动前:

在4次拖动到1个位置后:

import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class DragButtonsTest {

    ArrayList<JButton> buttons = new ArrayList<>();

    public DragButtonsTest() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel(new GridLayout(2, 2));

        ComponentDrag cd = new ComponentDrag(frame) {
            @Override
            protected void componentDropped(MouseEvent me) {
                HashMap<Integer, JButton> collisions = new HashMap<>();
                JButton draggedButton = (JButton) me.getSource();

                for (JButton btn : buttons) {//iterate through all buttons and get the number of collsions of each
                    if (btn != draggedButton) {//dont chck button we were dragging
                        int col = checkPerPixelCollision(draggedButton, btn);
                        System.out.println("Button " + btn.getText());
                        System.out.println(col);
                        collisions.put(col, btn);
                    }
                }

                //lets get the button which had most collisions
                int maxCollisions = 0;
                JButton intersectingButton = null;
                for (Map.Entry<Integer, JButton> entry : collisions.entrySet()) {
                    Integer collisionCount = entry.getKey();
                    JButton button = entry.getValue();
                    if (collisionCount > maxCollisions) {
                        maxCollisions = collisionCount;
                        intersectingButton = button;
                    }
                }

                boolean reLayout = false;

                if (maxCollisions > 0) {//check if there was any
                    System.out.println("Button " + draggedButton.getText() + " is intersecting more of Button " + intersectingButton.getText());
                    System.out.println("Collisions: " + maxCollisions);
                    reLayout = true;
                } else {
                    System.out.println("No change made");
                    reLayout = false;
                }

                ArrayList<JButton> tmpButtons = (ArrayList<JButton>) buttons.clone();//create clone of buttons

                if (reLayout) {//a button as moved and panel needs to be layed out
                    buttons.clear();//clear old buttons

                    for (JButton b : tmpButtons) {//re-order jbuttons
                        if (b == intersectingButton) {
                            buttons.add(draggedButton);
                        } else if (b == draggedButton) {
                            buttons.add(intersectingButton);
                        } else {
                            buttons.add(b);
                        }
                    }
                    panel.removeAll();//remove all buttons
                    for (JButton btn : buttons) {//iterate through all buttons and get the number of collsions of each
                        panel.add(btn);//re-add buttons according to arraylist
                    }
                    panel.revalidate();
                    panel.repaint();
                }
                super.componentDropped(me);

            }
        };

        for (int i = 0; i < 4; i++) {
            JButton b = new JButton(String.valueOf(i + 1));
            panel.add(b);
            buttons.add(b);
            cd.registerComponent(b);
        }

        frame.add(panel);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DragButtonsTest();
            }
        });
    }

    public HashSet<String> getMask(JButton e) {
        HashSet<String> mask = new HashSet<>();
        int pixel, a;
        BufferedImage bi = componentToImage(e); //gets the current image being shown

        for (int i = 0; i < bi.getWidth(); i++) { // for every (x,y) component in the given box, 
            for (int j = 0; j < bi.getHeight(); j++) {
                pixel = bi.getRGB(i, j); // get the RGB value of the pixel
                a = (pixel >> 24) & 0xff;
                if (a != 0) {  // if the alpha is not 0, it must be something other than transparent
                    mask.add((e.getX() + i) + "," + (e.getY() - j)); // add the absolute x and absolute y coordinates to our set
                }
            }
        }
        return mask;  //return our set
    }

    public static BufferedImage componentToImage(Component component) {
        BufferedImage img = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TRANSLUCENT);
        Graphics g = img.getGraphics();
        component.paintAll(g);
        return img;
    }

    // Returns true if there is a collision between object a and object b   
    public int checkPerPixelCollision(JButton b, JButton b2) {
        // This method detects to see if the images overlap at all. If they do, collision is possible
        int ax1 = (int) b2.getX();
        int ay1 = (int) b2.getY();

        int ax2 = ax1 + (int) b2.getWidth();
        int ay2 = ay1 + (int) b2.getHeight();

        int bx1 = (int) b.getX();
        int by1 = (int) b.getY();

        int bx2 = bx1 + (int) b.getWidth();

        int by2 = by1 + (int) b.getHeight();

        if (by2 < ay1 || ay2 < by1 || bx2 < ax1 || ax2 < bx1) {
            return 0; // Collision is impossible.
        } else { // Collision is possible.
            // get the masks for both images
            HashSet<String> maskPlayer1 = getMask(b2);
            HashSet<String> maskPlayer2 = getMask(b);
            maskPlayer1.retainAll(maskPlayer2);  // Check to see if any pixels in maskPlayer2 are the same as those in maskPlayer1
            if (maskPlayer1.size() > 0) {  // if so, than there exists at least one pixel that is the same in both images, thus
                return maskPlayer1.size();
            }
        }
        return 0;
    }
}

class ComponentDrag {

    private MouseAdapter ma;
    private ArrayList<JComponent> components = new ArrayList<>();
    private int startingX, startingY;
    private boolean autoLayout = true;
    private final JFrame container;
    private JPanel glassPane;
    boolean firstTime = true;
    private JComponent lastClickedContainer;

    public ComponentDrag(final JFrame container) {
        this.container = container;
        glassPane = new JPanel();
        glassPane.setOpaque(false);
        glassPane.setLayout(null);
        this.container.setGlassPane(glassPane);

        new Timer(10, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (container.isVisible()) {
                    glassPane.setVisible(true);
                    ((Timer) ae.getSource()).stop();
                }
            }
        }).start();

        ma = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                super.mousePressed(me);
                componentPressed(me);
            }

            @Override
            public void mouseReleased(MouseEvent me) {
                super.mouseReleased(me);
                componentDropped(me);
            }

            @Override
            public void mouseDragged(MouseEvent me) {
                super.mouseDragged(me);
                componentDragged(me);
            }
        };
    }

    public JComponent findDroppableUnderGlassPane(Point p, Container container, Component source) {
        JComponent c = null;
        Component[] comps = container.getComponents();
        for (Component com : comps) {
            if (com.getName() != null) {
                if (com.getName().equals("Droppable") && com instanceof JComponent) {
                    if (com.contains(SwingUtilities.convertPoint(source, p, com))) {
                        return (JComponent) com;
                    }
                } else if (com instanceof Container) {
                    findDroppableUnderGlassPane(p, (Container) com, source);
                }
            }
        }
        return c;
    }

    public boolean isAutoLayout() {
        return autoLayout;
    }

    public void setAutoLayout(boolean autoLayout) {
        this.autoLayout = autoLayout;
    }

    protected void componentDropped(MouseEvent me) {
        firstTime = true;
        Component droppedComponent = (Component) me.getSource();
        droppedComponent.setCursor(null);

        JComponent jc = findDroppableUnderGlassPane(me.getPoint(), container.getContentPane(), (Component) me.getSource());

        if (jc != null) {
            glassPane.removeAll();
            glassPane.revalidate();
            glassPane.repaint();

            jc.add(droppedComponent);
            System.out.println("Removed from glasspane and added to: " + jc);

            if (autoLayout) {
                if (lastClickedContainer != null) {
                    lastClickedContainer.revalidate();
                    lastClickedContainer.repaint();
                }
                droppedComponent.revalidate();
                droppedComponent.repaint();
                jc.revalidate();
                jc.repaint();
            }

        } else {
            glassPane.removeAll();
            glassPane.revalidate();
            glassPane.repaint();
            if (lastClickedContainer != null) {
                lastClickedContainer.add(droppedComponent);
                lastClickedContainer.revalidate();
                lastClickedContainer.repaint();
            }
        }
    }

    protected void componentPressed(MouseEvent me) {
        JComponent jc = findDroppableUnderGlassPane(me.getPoint(), container.getContentPane(), (Component) me.getSource());

        if (jc != null && jc.getName().equals("Droppable")) {
            lastClickedContainer = jc;
            System.out.println("Pressed: " + lastClickedContainer);
        }

        boolean clickedRegisteredComponent = false;

        Component clickedComponent = (Component) me.getSource();

        for (Component component : ComponentDrag.this.components) {
            if (component.equals(clickedComponent)) {
                clickedRegisteredComponent = true;
                break;
            }
        }

        if (clickedRegisteredComponent) {
            startingX = me.getX();
            startingY = me.getY();
            clickedComponent.setCursor(new Cursor(Cursor.MOVE_CURSOR));
        }
    }

    protected void componentDragged(MouseEvent me) {
        Component draggedComponent = (Component) me.getSource();

        if (firstTime && lastClickedContainer != null) {
            firstTime = false;
            lastClickedContainer.remove(draggedComponent);
            lastClickedContainer.revalidate();
            lastClickedContainer.repaint();
            glassPane.add(draggedComponent);
            glassPane.revalidate();
            glassPane.repaint();
            System.out.println("Removed from: " + lastClickedContainer + " \nAnd added to glasspane for dragging");
        }

        //MouseEvent will refire on each drag with the position being relative to the firing Component
        draggedComponent.setLocation((me.getX() - startingX) + draggedComponent.getLocation().x, (me.getY() - startingY) + draggedComponent.getLocation().y);
    }

    void registerComponent(JComponent draggableComp) {
        draggableComp.addMouseListener(ma);
        draggableComp.addMouseMotionListener(ma);
        components.add(draggableComp);
    }

    void deregisterComponent(JComponent draggableComp) {
        draggableComp.removeMouseListener(ma);
        draggableComp.removeMouseMotionListener(ma);
        components.remove(draggableComp);
    }
}


上面的代码有很多错误

  • 不要使用
    null
    /
    Absolute
    LayoutManager
  • 不要在组件和
    JFrame
    上调用
    setSize
    (如果实现了正确的布局管理器,您可以简单地添加组件并调用
    pack()
    on
    JFrame
  • 在EDT上创建和操作Swing组件。有关详细信息,请阅读
  • 从可见容器中添加/删除组件后,应在容器上调用
    revalidate()
    repaint()
我一直在开发自己的小
ComponentDrag
API,它允许我们在
JComponent
或多个
JComponent
s上拖动Swing组件。但这绝不是没有错误的(特别是多组件部分今天刚刚完成,所以你永远不知道)

您将创建
JButton
并添加到
JPanel

    JButton button = new JButton("Text");

    JPanel panel1 = new JPanel() {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    };
    panel1.add(button);
然后通过传递容器的参数,即
JFrame
/
JDialog
JWindow
来创建
ComponentDrag
类的实例,并注册可拖动组件并调用
setName(“可拖放”)
在您的
JPanel上
为了允许正确地进行拖放操作,尤其是在多个jcomponent等之间,您只能对希望能够拖放Swing组件的
JPanel等执行此操作:

    JFrame frame=..;//has access to setGlassPane same applies for JDialog and JWindow
    frame.add(panel1);

    ComponentDrag cd=new ComponentDrag(frame);
    cd.registerComponent(button);
    panel1.setName("Droppable");//needed for the dragging and dropping to work correctly especailly over multiple components
你的好消息:)

以下是用于分析目的的两个完整示例:

1) 显示移动
JButton
s的逻辑,并使用
ComponentDrag
并覆盖其
componentdrop
方法相应地重新定位:

拖动前:

在4次拖动到1个位置后:

import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class DragButtonsTest {

    ArrayList<JButton> buttons = new ArrayList<>();

    public DragButtonsTest() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel(new GridLayout(2, 2));

        ComponentDrag cd = new ComponentDrag(frame) {
            @Override
            protected void componentDropped(MouseEvent me) {
                HashMap<Integer, JButton> collisions = new HashMap<>();
                JButton draggedButton = (JButton) me.getSource();

                for (JButton btn : buttons) {//iterate through all buttons and get the number of collsions of each
                    if (btn != draggedButton) {//dont chck button we were dragging
                        int col = checkPerPixelCollision(draggedButton, btn);
                        System.out.println("Button " + btn.getText());
                        System.out.println(col);
                        collisions.put(col, btn);
                    }
                }

                //lets get the button which had most collisions
                int maxCollisions = 0;
                JButton intersectingButton = null;
                for (Map.Entry<Integer, JButton> entry : collisions.entrySet()) {
                    Integer collisionCount = entry.getKey();
                    JButton button = entry.getValue();
                    if (collisionCount > maxCollisions) {
                        maxCollisions = collisionCount;
                        intersectingButton = button;
                    }
                }

                boolean reLayout = false;

                if (maxCollisions > 0) {//check if there was any
                    System.out.println("Button " + draggedButton.getText() + " is intersecting more of Button " + intersectingButton.getText());
                    System.out.println("Collisions: " + maxCollisions);
                    reLayout = true;
                } else {
                    System.out.println("No change made");
                    reLayout = false;
                }

                ArrayList<JButton> tmpButtons = (ArrayList<JButton>) buttons.clone();//create clone of buttons

                if (reLayout) {//a button as moved and panel needs to be layed out
                    buttons.clear();//clear old buttons

                    for (JButton b : tmpButtons) {//re-order jbuttons
                        if (b == intersectingButton) {
                            buttons.add(draggedButton);
                        } else if (b == draggedButton) {
                            buttons.add(intersectingButton);
                        } else {
                            buttons.add(b);
                        }
                    }
                    panel.removeAll();//remove all buttons
                    for (JButton btn : buttons) {//iterate through all buttons and get the number of collsions of each
                        panel.add(btn);//re-add buttons according to arraylist
                    }
                    panel.revalidate();
                    panel.repaint();
                }
                super.componentDropped(me);

            }
        };

        for (int i = 0; i < 4; i++) {
            JButton b = new JButton(String.valueOf(i + 1));
            panel.add(b);
            buttons.add(b);
            cd.registerComponent(b);
        }

        frame.add(panel);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DragButtonsTest();
            }
        });
    }

    public HashSet<String> getMask(JButton e) {
        HashSet<String> mask = new HashSet<>();
        int pixel, a;
        BufferedImage bi = componentToImage(e); //gets the current image being shown

        for (int i = 0; i < bi.getWidth(); i++) { // for every (x,y) component in the given box, 
            for (int j = 0; j < bi.getHeight(); j++) {
                pixel = bi.getRGB(i, j); // get the RGB value of the pixel
                a = (pixel >> 24) & 0xff;
                if (a != 0) {  // if the alpha is not 0, it must be something other than transparent
                    mask.add((e.getX() + i) + "," + (e.getY() - j)); // add the absolute x and absolute y coordinates to our set
                }
            }
        }
        return mask;  //return our set
    }

    public static BufferedImage componentToImage(Component component) {
        BufferedImage img = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TRANSLUCENT);
        Graphics g = img.getGraphics();
        component.paintAll(g);
        return img;
    }

    // Returns true if there is a collision between object a and object b   
    public int checkPerPixelCollision(JButton b, JButton b2) {
        // This method detects to see if the images overlap at all. If they do, collision is possible
        int ax1 = (int) b2.getX();
        int ay1 = (int) b2.getY();

        int ax2 = ax1 + (int) b2.getWidth();
        int ay2 = ay1 + (int) b2.getHeight();

        int bx1 = (int) b.getX();
        int by1 = (int) b.getY();

        int bx2 = bx1 + (int) b.getWidth();

        int by2 = by1 + (int) b.getHeight();

        if (by2 < ay1 || ay2 < by1 || bx2 < ax1 || ax2 < bx1) {
            return 0; // Collision is impossible.
        } else { // Collision is possible.
            // get the masks for both images
            HashSet<String> maskPlayer1 = getMask(b2);
            HashSet<String> maskPlayer2 = getMask(b);
            maskPlayer1.retainAll(maskPlayer2);  // Check to see if any pixels in maskPlayer2 are the same as those in maskPlayer1
            if (maskPlayer1.size() > 0) {  // if so, than there exists at least one pixel that is the same in both images, thus
                return maskPlayer1.size();
            }
        }
        return 0;
    }
}

class ComponentDrag {

    private MouseAdapter ma;
    private ArrayList<JComponent> components = new ArrayList<>();
    private int startingX, startingY;
    private boolean autoLayout = true;
    private final JFrame container;
    private JPanel glassPane;
    boolean firstTime = true;
    private JComponent lastClickedContainer;

    public ComponentDrag(final JFrame container) {
        this.container = container;
        glassPane = new JPanel();
        glassPane.setOpaque(false);
        glassPane.setLayout(null);
        this.container.setGlassPane(glassPane);

        new Timer(10, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (container.isVisible()) {
                    glassPane.setVisible(true);
                    ((Timer) ae.getSource()).stop();
                }
            }
        }).start();

        ma = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                super.mousePressed(me);
                componentPressed(me);
            }

            @Override
            public void mouseReleased(MouseEvent me) {
                super.mouseReleased(me);
                componentDropped(me);
            }

            @Override
            public void mouseDragged(MouseEvent me) {
                super.mouseDragged(me);
                componentDragged(me);
            }
        };
    }

    public JComponent findDroppableUnderGlassPane(Point p, Container container, Component source) {
        JComponent c = null;
        Component[] comps = container.getComponents();
        for (Component com : comps) {
            if (com.getName() != null) {
                if (com.getName().equals("Droppable") && com instanceof JComponent) {
                    if (com.contains(SwingUtilities.convertPoint(source, p, com))) {
                        return (JComponent) com;
                    }
                } else if (com instanceof Container) {
                    findDroppableUnderGlassPane(p, (Container) com, source);
                }
            }
        }
        return c;
    }

    public boolean isAutoLayout() {
        return autoLayout;
    }

    public void setAutoLayout(boolean autoLayout) {
        this.autoLayout = autoLayout;
    }

    protected void componentDropped(MouseEvent me) {
        firstTime = true;
        Component droppedComponent = (Component) me.getSource();
        droppedComponent.setCursor(null);

        JComponent jc = findDroppableUnderGlassPane(me.getPoint(), container.getContentPane(), (Component) me.getSource());

        if (jc != null) {
            glassPane.removeAll();
            glassPane.revalidate();
            glassPane.repaint();

            jc.add(droppedComponent);
            System.out.println("Removed from glasspane and added to: " + jc);

            if (autoLayout) {
                if (lastClickedContainer != null) {
                    lastClickedContainer.revalidate();
                    lastClickedContainer.repaint();
                }
                droppedComponent.revalidate();
                droppedComponent.repaint();
                jc.revalidate();
                jc.repaint();
            }

        } else {
            glassPane.removeAll();
            glassPane.revalidate();
            glassPane.repaint();
            if (lastClickedContainer != null) {
                lastClickedContainer.add(droppedComponent);
                lastClickedContainer.revalidate();
                lastClickedContainer.repaint();
            }
        }
    }

    protected void componentPressed(MouseEvent me) {
        JComponent jc = findDroppableUnderGlassPane(me.getPoint(), container.getContentPane(), (Component) me.getSource());

        if (jc != null && jc.getName().equals("Droppable")) {
            lastClickedContainer = jc;
            System.out.println("Pressed: " + lastClickedContainer);
        }

        boolean clickedRegisteredComponent = false;

        Component clickedComponent = (Component) me.getSource();

        for (Component component : ComponentDrag.this.components) {
            if (component.equals(clickedComponent)) {
                clickedRegisteredComponent = true;
                break;
            }
        }

        if (clickedRegisteredComponent) {
            startingX = me.getX();
            startingY = me.getY();
            clickedComponent.setCursor(new Cursor(Cursor.MOVE_CURSOR));
        }
    }

    protected void componentDragged(MouseEvent me) {
        Component draggedComponent = (Component) me.getSource();

        if (firstTime && lastClickedContainer != null) {
            firstTime = false;
            lastClickedContainer.remove(draggedComponent);
            lastClickedContainer.revalidate();
            lastClickedContainer.repaint();
            glassPane.add(draggedComponent);
            glassPane.revalidate();
            glassPane.repaint();
            System.out.println("Removed from: " + lastClickedContainer + " \nAnd added to glasspane for dragging");
        }

        //MouseEvent will refire on each drag with the position being relative to the firing Component
        draggedComponent.setLocation((me.getX() - startingX) + draggedComponent.getLocation().x, (me.getY() - startingY) + draggedComponent.getLocation().y);
    }

    void registerComponent(JComponent draggableComp) {
        draggableComp.addMouseListener(ma);
        draggableComp.addMouseMotionListener(ma);
        components.add(draggableComp);
    }

    void deregisterComponent(JComponent draggableComp) {
        draggableComp.removeMouseListener(ma);
        draggableComp.removeMouseMotionListener(ma);
        components.remove(draggableComp);
    }
}


为了更快地获得更好的帮助,请发布@amod0017,SSCCE将回答该问题。另外,请修复卡住的“?”键。@AndrewThompson是的,我同意…+1对Andrews的评论和问题最后一个SSCCE:)为了更快地获得更好的帮助,请发布一条。@amod0017 SSCCE将回答该问题。另外,请修复卡在“?”键。@AndrewThompson是的,我同意…+1对Andrews的评论,问题最后是SSCCE:)+1,用于玻璃窗格;另请参见此备选方案。@trashgood一个不错的示例,也可以使用glasspane主要用于拖动多个JPanel ect+1以获得glasspane;另请参见此备选方案。@trashgood很好的示例,也可以使用glasspane主要用于在多个JPanel等上拖动