我是Swing的新手。我需要使用swing实现拖放。在当前帧中包含不同的面板,即Center Panel& LeftPanel有子面板,即控制面板,属性面板。我试图将标签/ jbuttons /图像从控制面板拖放到中心面板。这些面板使用BorderLayout
我尝试使用鼠标事件拖放。我将鼠标列表器放在控制面板上。当我将标签/图像放入中心面板时。它读取-X& Y Cooridnates并检查代码
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];
}
}
}
});
}
请告诉我这里有什么遗漏。什么是实现拖放功能的最佳方法?
答案 0 :(得分:4)