我是Swing的新手。我需要使用摇摆来实现拖放。在当前框架中包含不同的面板,即中心面板
我尝试用鼠标事件拖放。我把鼠标侦听器放在控制面板上。当我把标签/图像放在中间时panel.It读到-X
private void mak_lis(final SLabel l) {
l.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent m) {
System.out.println("mak_lis Mouse mousePressed");
setCursor(yd);
// l.setBorder(new MatteBorder(1,1,1,1,Color.black));
}
public void mouseReleased(MouseEvent m) {
l.setBorder(null);
setCursor(dc);
System.out.println("mak_lis Mouse mouseReleased");
int x = -(m.getX() + l.getX() - leftPanel.getX());
int y = m.getY() + l.getY() + leftPanel.getY();
System.out.println("mak_lis Mouse mouseReleased" + "x" + x+ "y" + y);
if (y > 0 && x > 0 && y < leftPanel.getHeight() && x < leftPanel.getWidth()) {
leftPanel.add(new_lab(l, x, y));
leftPanel.repaint();
Component[] components1 = leftPanel.getComponents();
Component component = null;
for (int i = 0; i < components1.length; i++) {
// System.out.println("components iii"+ components1[i]);
component = components1[i];
}
}
}
});
}
请告诉我这里是否有任何遗漏。实现拖放功能的最佳方法是什么?
这不是Java/摇摆中拖放的实现方式
SO上有很多例子…
我还建议你检查如何拖放Java