gpt4 book ai didi

java - 扩展类 JLabel 以添加属性拖动的问题

转载 作者:行者123 更新时间:2023-11-30 09:50:38 25 4
gpt4 key购买 nike

我有类 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)类 LabelEasy 扩展 JLabel { 私有(private)鼠标适配器移动鼠标适配器; 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);
}

但它像以前的变体一样工作。

最佳答案

我认为你做错了。 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);
}
}

关于java - 扩展类 JLabel 以添加属性拖动的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5071508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com