扩展类 JLabel 以添加属性拖动时出现问题

发布于 2024-10-18 14:23:15 字数 2205 浏览 1 评论 0原文

我有类 JLabelExtended,它扩展了类 javax.swing.JLabel。 我扩展了它,因为我想添加使用鼠标拖动的属性。 这是我的代码:

public class JLabelExtended extends JLabel {
private MouseMotionAdapter mouseMotionAdapter;

private JLabelExtended jLabelExtended;

public LabelEasy(String text) {
    super(text);
    jLabelExtended = this;

    mouseMotionAdapter = new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            System.out.println(e.getX() + "  :   " + e.getY());
            jLabelExtended.setLocation(e.getX(), e.getY()
            );
        }
    };

    jLabelExtended.addMouseMotionListener(mouseMotionAdapter);
}

}

这是拖动标签后的控制台部分:

163  :   163
144  :   -87
163  :   162
144  :   -88
163  :   161
144  :   -89

我有几个问题:

  1. 为什么 e.getY() 会得到负面结果?

  2. 当我拖动标签时,会出现拖动到我的标签附近的标签副本。我该如何修复它?

  3. 当我拖动标签时,它拖动得非常慢。例如:当我将光标移动到 10 个点时,我的标签仅移动到 5 个点。我该如何解决这个问题?

预先感谢

以下是扩展 JLabel 的另一种方法:

public class LabelEasy extends JLabel { 私有鼠标适配器 moveMouseAdapter; 私有MouseMotionAdapter mouseMotionAdapter;

private LabelEasy jLabelExtended;

private int xAdjustment, yAdjustment;
Boolean count = false;

public LabelEasy(String text) {
    super(text);
    jLabelExtended = this;

    moveMouseAdapter = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == 1) {
                xAdjustment = e.getX();
                yAdjustment = e.getY();
            }
        }
    };

    mouseMotionAdapter = new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            if (count) {
                System.out.println(e.getX() + "  :   " + e.getY());
                jLabelExtended.setLocation(xAdjustment + e.getX(), yAdjustment + e.getY());
                count = false;
            } else {
                count = true;
            }
            ;
        }
    };

    jLabelExtended.addMouseMotionListener(mouseMotionAdapter);
    jLabelExtended.addMouseListener(moveMouseAdapter);
}

它的工作原理与之前的变体类似。

I have class JLabelExtended, which extends class javax.swing.JLabel.
I extend it, because I want to add property dragging using mouse.
Here is my code:

public class JLabelExtended extends JLabel {
private MouseMotionAdapter mouseMotionAdapter;

private JLabelExtended jLabelExtended;

public LabelEasy(String text) {
    super(text);
    jLabelExtended = this;

    mouseMotionAdapter = new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            System.out.println(e.getX() + "  :   " + e.getY());
            jLabelExtended.setLocation(e.getX(), e.getY()
            );
        }
    };

    jLabelExtended.addMouseMotionListener(mouseMotionAdapter);
}

}

This is console part after label dragged:

163  :   163
144  :   -87
163  :   162
144  :   -88
163  :   161
144  :   -89

I have several questions:

  1. Why e.getY() takes negative results?

  2. When I drag my label there are appeares copy of label which drags near my label. How can I fix it?

  3. When I drag my label, it drags very slowly.For example: when I move my cursor on 10 points my label moves only on 5 point. How can I fix it?

Thanks in advance

Here are else one way to extend JLabel:

public class LabelEasy extends JLabel {
private MouseAdapter moveMouseAdapter;
private MouseMotionAdapter mouseMotionAdapter;

private LabelEasy jLabelExtended;

private int xAdjustment, yAdjustment;
Boolean count = false;

public LabelEasy(String text) {
    super(text);
    jLabelExtended = this;

    moveMouseAdapter = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == 1) {
                xAdjustment = e.getX();
                yAdjustment = e.getY();
            }
        }
    };

    mouseMotionAdapter = new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            if (count) {
                System.out.println(e.getX() + "  :   " + e.getY());
                jLabelExtended.setLocation(xAdjustment + e.getX(), yAdjustment + e.getY());
                count = false;
            } else {
                count = true;
            }
            ;
        }
    };

    jLabelExtended.addMouseMotionListener(mouseMotionAdapter);
    jLabelExtended.addMouseListener(moveMouseAdapter);
}

}

But it works like previous variant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然#的苍凉 2024-10-25 14:23:15

我认为你做错了。 MouseMotionListener被添加到JLabel中,并且它的位置相对于JLabel,而不是保存JLabel的Container,因此该信息对帮助您拖动它是没有用的。您可能希望使用 MouseAdapter 并将其添加为 MouseListener 和 MouseMotionListener。在 mousePressed 上,获取 JLabel 和鼠标相对于屏幕的位置,然后将其用于在 mouseDragged 上进行拖动。我自己不会扩展 JLabel 来执行此操作,而宁愿仅使用常规 JLabel,但这是我的偏好。

编辑:当我处理鼠标相对于屏幕的位置(通过调用 getLocationOnScreen)和 JLabel 相对于其容器的位置(通过调用 getLocation)时,它对我来说效果更好。例如,

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class DragLabelEg {
    private static final String[] LABEL_STRINGS = { "Do", "Re", "Me", "Fa",
            "So", "La", "Ti" };
    private static final int HEIGHT = 400;
    private static final int WIDTH = 600;
    private static final Dimension MAIN_PANEL_SIZE = new Dimension(WIDTH,
            HEIGHT);
    private static final int LBL_WIDTH = 60;
    private static final int LBL_HEIGHT = 40;
    private static final Dimension LABEL_SIZE = new Dimension(LBL_WIDTH,
            LBL_HEIGHT);
    private JPanel mainPanel = new JPanel();
    private Random random = new Random();

    public DragLabelEg() {
        mainPanel.setPreferredSize(MAIN_PANEL_SIZE);
        mainPanel.setLayout(null);

        MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
        for (int i = 0; i < LABEL_STRINGS.length; i++) {
            JLabel label = new JLabel(LABEL_STRINGS[i], SwingConstants.CENTER);
            label.setSize(LABEL_SIZE);
            label.setOpaque(true);
            label.setLocation(random.nextInt(WIDTH - LBL_WIDTH),
                    random.nextInt(HEIGHT - LBL_HEIGHT));
            label.setBackground(new Color(150 + random.nextInt(105),
                    150 + random.nextInt(105), 150 + random.nextInt(105)));
            label.addMouseListener(myMouseAdapter);
            label.addMouseMotionListener(myMouseAdapter);

            mainPanel.add(label);
        }
    }

    public JComponent getMainPanel() {
        return mainPanel;
    }

    private class MyMouseAdapter extends MouseAdapter {
        private Point initLabelLocation = null;
        private Point initMouseLocationOnScreen = null;

        @Override
        public void mousePressed(MouseEvent e) {
            JLabel label = (JLabel)e.getSource();
            // get label's initial location relative to its container
            initLabelLocation = label.getLocation();

            // get Mouse's initial location relative to the screen 
            initMouseLocationOnScreen = e.getLocationOnScreen();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            initLabelLocation = null;
            initMouseLocationOnScreen = null;
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            // if not dragging a JLabel
            if (initLabelLocation == null || initMouseLocationOnScreen == null) {
                return;
            }
            JLabel label = (JLabel)e.getSource();

            // get mouse's new location relative to the screen
            Point mouseLocation = e.getLocationOnScreen();

            // and see how this differs from the initial location.
            int deltaX = mouseLocation.x - initMouseLocationOnScreen.x;
            int deltaY = mouseLocation.y - initMouseLocationOnScreen.y;

            // change label's position by the same difference, the "delta" vector
            int labelX = initLabelLocation.x + deltaX;
            int labelY = initLabelLocation.y + deltaY;

            label.setLocation(labelX, labelY);
        }
    }

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

    private static void createGui() {
        JFrame frame = new JFrame("App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new DragLabelEg().getMainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

I think you're doing it wrong. The MouseMotionListener is added to the JLabel and its location is relative to the JLabel, not the Container which holds the JLabel, so the information is useless to help you drag it. You may wish to use a MouseAdapter and add it both as a MouseListener and a MouseMotionListener. On mousePressed, get the location of the JLabel and the mouse relative to the screen and then use that for your dragging on mouseDragged. Myself, I wouldn't extend JLabel to do this but would rather just use a regular JLabel, but that's my preference.

Edit: it worked better for me when I dealt with the mouse's position relative to the screen (by calling getLocationOnScreen) and the JLabel's position relative to its Container (by calling getLocation). For e.g.,

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class DragLabelEg {
    private static final String[] LABEL_STRINGS = { "Do", "Re", "Me", "Fa",
            "So", "La", "Ti" };
    private static final int HEIGHT = 400;
    private static final int WIDTH = 600;
    private static final Dimension MAIN_PANEL_SIZE = new Dimension(WIDTH,
            HEIGHT);
    private static final int LBL_WIDTH = 60;
    private static final int LBL_HEIGHT = 40;
    private static final Dimension LABEL_SIZE = new Dimension(LBL_WIDTH,
            LBL_HEIGHT);
    private JPanel mainPanel = new JPanel();
    private Random random = new Random();

    public DragLabelEg() {
        mainPanel.setPreferredSize(MAIN_PANEL_SIZE);
        mainPanel.setLayout(null);

        MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
        for (int i = 0; i < LABEL_STRINGS.length; i++) {
            JLabel label = new JLabel(LABEL_STRINGS[i], SwingConstants.CENTER);
            label.setSize(LABEL_SIZE);
            label.setOpaque(true);
            label.setLocation(random.nextInt(WIDTH - LBL_WIDTH),
                    random.nextInt(HEIGHT - LBL_HEIGHT));
            label.setBackground(new Color(150 + random.nextInt(105),
                    150 + random.nextInt(105), 150 + random.nextInt(105)));
            label.addMouseListener(myMouseAdapter);
            label.addMouseMotionListener(myMouseAdapter);

            mainPanel.add(label);
        }
    }

    public JComponent getMainPanel() {
        return mainPanel;
    }

    private class MyMouseAdapter extends MouseAdapter {
        private Point initLabelLocation = null;
        private Point initMouseLocationOnScreen = null;

        @Override
        public void mousePressed(MouseEvent e) {
            JLabel label = (JLabel)e.getSource();
            // get label's initial location relative to its container
            initLabelLocation = label.getLocation();

            // get Mouse's initial location relative to the screen 
            initMouseLocationOnScreen = e.getLocationOnScreen();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            initLabelLocation = null;
            initMouseLocationOnScreen = null;
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            // if not dragging a JLabel
            if (initLabelLocation == null || initMouseLocationOnScreen == null) {
                return;
            }
            JLabel label = (JLabel)e.getSource();

            // get mouse's new location relative to the screen
            Point mouseLocation = e.getLocationOnScreen();

            // and see how this differs from the initial location.
            int deltaX = mouseLocation.x - initMouseLocationOnScreen.x;
            int deltaY = mouseLocation.y - initMouseLocationOnScreen.y;

            // change label's position by the same difference, the "delta" vector
            int labelX = initLabelLocation.x + deltaX;
            int labelY = initLabelLocation.y + deltaY;

            label.setLocation(labelX, labelY);
        }
    }

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

    private static void createGui() {
        JFrame frame = new JFrame("App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new DragLabelEg().getMainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文