2

我有一个 JPanel,上面有 2 个以上的 JLable,我希望能够抓取一个标签,然后将其移动到 JPanel 上的不同位置。我怎样才能做到这一点?我唯一能找到的就是将标签从组件“A”移动到组件“B”,而不是在面板上移动它。

4

4 回答 4

3

开始玩这个:

public class ComponentDragger extends MouseAdapter {

     private Component target;

    /**
     * {@inheritDoc}
     */
    @Override
    public void mousePressed(MouseEvent e) {
        Container container = (Container) e.getComponent();
        for (Component c : container.getComponents()) {
            if (c.getBounds().contains(e.getPoint())) {
                target = c;
                break;
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void mouseDragged(MouseEvent e) {
        if (target != null) {
            target.setBounds(e.getX(), e.getY(), target.getWidth(), target.getHeight());
            e.getComponent().repaint();
            }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void mouseReleased(MouseEvent e) {
        target = null;
    }

    public static void main(String[] args) {
        JLabel label = new JLabel("Drag Me");
        JPanel panel = new JPanel();
        panel.add(label);
        ComponentDragger dragger = new ComponentDragger();
        panel.addMouseListener(dragger);
        panel.addMouseMotionListener(dragger);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(1024, 768);
        f.add(panel);
        f.setVisible(true);
        panel.setLayout(null);
        f.setState(Frame.MAXIMIZED_BOTH);
    }
}
于 2012-12-02T21:21:21.723 回答
2

这是另一个示例,其中 MouseListener 和 MouseMotionListener 位于 JLabels 本身上。为此,它需要知道鼠标在屏幕上的位置与最初按下鼠标时它在屏幕上的初始位置。

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class MovingLabels {
   private static final int PREF_W = 800;
   private static final int PREF_H = 600;

   private static void createAndShowGui() {
      Random random = new Random();
      final JPanel panel = new JPanel();

      Color[] colors = {Color.red, Color.orange, Color.yellow, Color.green, Color.blue, Color.cyan};
      panel.setPreferredSize(new Dimension(PREF_W, PREF_H)); // sorry kleopatra
      panel.setLayout(null);

      MyMouseAdapter myMouseAdapter = new MyMouseAdapter();

      for (int i = 0; i < colors.length; i++) {
         Color c = colors[i];
         JLabel label = new JLabel("Label " + (i + 1));
         Border outsideBorder = new LineBorder(Color.black);
         int eb = 10;
         Border insideBorder = new EmptyBorder(eb, eb, eb, eb);
         label.setBorder(BorderFactory.createCompoundBorder(outsideBorder , insideBorder));
         label.setSize(label.getPreferredSize());
         label.setBackground(c);
         label.setOpaque(true);
         int x = random.nextInt(PREF_W - 200) + 100;
         int y = random.nextInt(PREF_H - 200) + 100;
         label.setLocation(x, y);
         label.addMouseListener(myMouseAdapter);
         label.addMouseMotionListener(myMouseAdapter);
         panel.add(label);
      }

      JFrame frame = new JFrame("MovingLabels");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(panel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

class MyMouseAdapter extends MouseAdapter {

   private Point initialLoc;
   private Point initialLocOnScreen;

   @Override
   public void mousePressed(MouseEvent e) {
      Component comp = (Component)e.getSource();
      initialLoc = comp.getLocation();
      initialLocOnScreen = e.getLocationOnScreen();
   }

   @Override
   public void mouseReleased(MouseEvent e) {
      Component comp = (Component)e.getSource();
      Point locOnScreen = e.getLocationOnScreen();

      int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x;
      int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y;
      comp.setLocation(x, y);
   }

   @Override
   public void mouseDragged(MouseEvent e) {
      Component comp = (Component)e.getSource();
      Point locOnScreen = e.getLocationOnScreen();

      int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x;
      int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y;
      comp.setLocation(x, y);
   }
}
于 2012-12-02T21:24:19.003 回答
1

您可能会因为标签本身而得到它。尝试观察面板的坐标。它应该工作

于 2012-12-02T21:30:45.737 回答
-1

这是我想要的:

public class LayerItem extends JLabel{

    protected int lblYPt = 0;

    public LayerItem(JPanel layers){
        this.addMouseListener(new MouseAdapter(){
            @Override
             public void mousePressed(MouseEvent evt){
                 lblMousePressed(evt);
             }
        });

        this.addMouseMotionListener(new MouseAdapter(){
            @Override
            public void mouseDragged(MouseEvent evt){
                lblMouseDragged(evt);
            }
        });
    }

    public void lblMousePressed(MouseEvent evt){
        lblYPt = evt.getY();
    }

    public void lblMouseDragged(MouseEvent evt){
        Component parent = evt.getComponent().getParent();
        Point mouse = parent.getMousePosition();
        try{
            if(mouse.y - lblYPt >= 30){
                this.setBounds(0, mouse.y - lblYPt, 198, 50);
            }
        }catch(Exception e){
        }
    }
}
于 2012-12-02T22:18:29.927 回答