【问题标题】:Moving the label in Java在 Java 中移动标签
【发布时间】:2022-01-15 05:25:04
【问题描述】:

我想制作一个标签并在屏幕上移动它。我试图找到一些视频和其他资源以找到如何做到这一点,但无法很好地理解它们,或者他们使用了我从未听说过的东西。 所以我做了这段代码(框架扩展了JFrame并实现了MouseListener):

这是主类:

public class Main {

    public static void main(String[] args) {
        new Frame();
    }
}

这是带有代码的类:

public class Frame extends JFrame implements MouseListener{

    JLabel label;

    Frame() {
        label = new JLabel();

        label.setBounds(800, 200, 200, 200);
        label.setBackground(Color.RED);
        label.setOpaque(true);
        label.addMouseListener(this);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(1200, 700);
        this.setLayout(null);
        this.add(label);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        Point x = e.getPoint();
        label.setLocation(x);
        System.out.println(x);
    }

当我尝试移动标签时,标签会移动,但不是在我想要的位置。 只有有时,标签会朝我想要的方向移动,但大多数时候,它会朝任意随机方向移动。即使我只是点击,它也会移动。 但是,即使标签向我想要的方向移动,它也会在该方向上随机长度。

【问题讨论】:

  • 1) 同意 MRE 上的@c0der。 2)但我觉得整个方法都不是最优的。我不会使用null 布局和移动组件,而是自定义绘制面板并使用基本字符串来替换标签。使用getLocationOnScreen 似乎也容易出错。我总是使用感兴趣的组件中的位置。 3)“我试图找到一些视频和其他来源”忘记视频,至于“其他来源”,我强烈推荐Java DocsJava Tutorial
  • 您需要使用getPoint。它不会是随机的,如果你打印坐标,你就会知道它是对的。您还将知道您希望它移动到的位置,并基于此更改您的逻辑。
  • 我刚刚做了一个名为 Frame 的类,扩展了 JFrame 并实现了 MouseListener 请提供 mre
  • “我刚开了一个课..” 所以把这个简短的(完整的)代码发布为minimal reproducible example。你的陈述在这里读起来像 “帮助我,你只需要..” 这是我停止阅读的地方,因为我们不需要做任何事情。我们的兴趣纯粹是学术性的,周围还有很多其他人确实听从我们的建议并让我们轻松提供帮助。

标签: java swing user-interface label mouselistener


【解决方案1】:

您可以通过使用MouseListener 来跟踪鼠标按下的时间并获取标签的坐标,同时使用MouseMotionAdapter 跟踪鼠标移动(或拖动)的时间来帮助您设置新的标签的位置。

更新: 要添加有关其工作原理的更多信息,e.getLocationOnScreen().x 根据计算机屏幕返回鼠标按下的 X 坐标,而 label.getX() 根据已添加到的 Java Swing 容器返回标签的 X 坐标;在这种情况下,即JFrame。因此,必须从前一个值中减去该值才能获得屏幕上标签的实际 X 坐标。同样适用于 Y 坐标。获得两个坐标后,您可以使用label.setLocation() 方法更改标签的位置,从而在屏幕上移动标签。

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

public class LabelDragExample extends JFrame {
    JLabel label;
    int x, y;

    public LabelDragExample() {
        label = new JLabel();
        label.setBounds(800, 200, 200, 200);
        label.setBackground(Color.RED);
        label.setOpaque(true);
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                x = e.getLocationOnScreen().x - label.getX();
                y = e.getLocationOnScreen().y - label.getY();
            }
        });
        label.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                label.setLocation(e.getLocationOnScreen().x - x, e.getLocationOnScreen().y - y);
                x = e.getLocationOnScreen().x - label.getX();
                y = e.getLocationOnScreen().y - label.getY();

            }
        });
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(1200, 700);
        this.setLayout(null);
        this.setVisible(true);
        this.add(label);
        this.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {

        new LabelDragExample();

    }
}

【讨论】:

  • 谢谢。它现在确实起作用了。但是,我编写的代码中是什么东西让标签移动,即使只是点击并让它如此随机地移动
  • 是的。我使用了 mouseReleased 方法,这意味着它只有在我释放按钮时才会被调用,同时标签根本不会移动。我在问你是否能说出为什么标签上的电线是错误的
  • @YashwardhanBisht,在这种方法中,标签通过mouseDragged 事件沿鼠标指针移动,因此您看不到您面临的实际问题,因为这里鼠标当前位置的差异。原始位置添加到标签当前位置[x = mLOS.orig.x - label.x; mLOS.new.x - x = mLOS.new.x - (mLOS.orig.x - label.x) = mLOS.new.x - mLOS.orig.x + label.x = diff + label.x] 请参考我的回复stackoverflow.com/a/70727525/12944233,其中显示了释放鼠标按钮时将标签与鼠标指针对齐的正确方法。
【解决方案2】:

我观察到鼠标在容器上的 (0, 0) 位置和标签的 (0, 0) 位置之间存在一些偏移。在附加的代码中,我将鼠标指针设置为标签在屏幕上的位置点,但您会注意到它并没有与标签的 (0, 0) 位置完全对齐。我们需要调整这个偏移量。一旦我们调整了偏移量,一切都会好起来的。你可以试试附上的代码:

package com.example.temp;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Main extends JFrame implements MouseListener {
    JLabel label;
    Point offset = new Point();

    Main() {
        addMouseListener(this);

        label = new JLabel();
        label.setBounds(0, 0, 200, 200);
        label.setBackground(Color.RED);
        label.setOpaque(true);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(1200, 700);
        this.setLayout(null);
        this.add(label);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        try {
            Robot robot = new Robot();
            Point labelOSPoint = label.getLocationOnScreen();
            robot.mouseMove(labelOSPoint.x, labelOSPoint.y);
        } catch (AWTException ignored) {
        }
        Point labelPoint = label.getLocationOnScreen();
        Point containerPoint = this.getLocationOnScreen();
        offset.x = labelPoint.x - containerPoint.x;
        offset.y = labelPoint.y - containerPoint.y;
        System.out.println("offset x = " + offset.x + ", y = " + offset.y);
        label.setText("Location: x = " + label.getX() + ", y = " + label.getY());
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        Point mousePoint = e.getPoint();
        System.out.println("mousePoint x = " + mousePoint.x + ", y = " + mousePoint.y);
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
        Point mousePoint = e.getPoint();
        label.setLocation(mousePoint.x - offset.x, mousePoint.y - offset.y);
        label.setText("Location: x = " + label.getX() + ", y = " + label.getY());
    }

    public static void main(String[] args) {
        new Main();
    }
}

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 2018-03-05
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多