Java 扩展类JLabel以添加到属性中时出现问题
我有一个类JLabelextend,它扩展了类javax.swing.JLabel。 我扩展了它,因为我想使用鼠标添加属性拖动。 这是我的密码:Java 扩展类JLabel以添加到属性中时出现问题,java,swing,mouseevent,jlabel,jcomponent,Java,Swing,Mouseevent,Jlabel,Jcomponent,我有一个类JLabelextend,它扩展了类javax.swing.JLabel。 我扩展了它,因为我想使用鼠标添加属性拖动。 这是我的密码: public class JLabelExtended extends JLabel { private MouseMotionAdapter mouseMotionAdapter; private JLabelExtended jLabelExtended; public LabelEasy(String text) { super(te
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
我有几个问题:
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的容器,因此这些信息对您拖动它毫无帮助。您可能希望使用MouseAdapter并将其添加为MouseListener和MouseMotionListener。在mousePressed上,获取JLabel和鼠标相对于屏幕的位置,然后使用该位置在MouseDraged上进行拖动。对于我自己,我不会扩展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);
}
}
import java.awt.*;
导入java.awt.event.*;
导入java.util.Random;
导入javax.swing.*;
公共级DragLabelEg{
私有静态最终字符串[]LABEL_STRINGS={“Do”、“Re”、“Me”、“Fa”,
“So”、“La”、“Ti”};
专用静态最终内部高度=400;
专用静态最终整数宽度=600;
专用静态最终尺寸主面板尺寸=新尺寸(宽度,
高度);
专用静态最终整数LBL_宽度=60;
专用静态最终内部LBL_高度=40;
专用静态最终尺寸标签尺寸=新尺寸(LBL宽度,
LBL_高度);
private JPanel mainPanel=new JPanel();
私有随机=新随机();
公共DragLabelEg(){
mainPanel.setPreferredSize(主面板大小);
mainPanel.setLayout(空);
MyMouseAdapter MyMouseAdapter=新的MyMouseAdapter();
for(int i=0;i
这些位置的顺序可疑。X在163和144之间切换,而两个分散的Y序列每次减少1。我想知道你是否因为标签移动而被解雇了?尝试的随机想法:创建标签的部分透明视图,在旧标签保持在位的同时移动该视图。或者将侦听器附加到标签的容器上。我试着去做,你说什么。但这并不能解释为什么我的标签副本出现在附近,以及为什么坐标变化如此奇怪。作为我的意思的示例,请参阅上面的编辑和代码。非常感谢。它真的帮助了我