将listview中的值比如说111,拖拽到pictureview中显示出来后,继续拖拽listview的值222,使其可以选择放置的位置,即鼠标按下时在已经放置的数据111的前面,222就显示在111的前面,如果鼠标按下时在111的后面,222就显示在111的后面,详细代码该如何实现
时间: 2023-05-14 12:07:22 浏览: 129
您可以使用拖放事件和适当的布局管理器来实现此功能。以下是一个示例代码片段,可以帮助您开始:
```
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import javax.swing.*;
public class ListViewPictureViewExample extends JFrame implements DragGestureListener, DropTargetListener {
private JList<String> listView;
private JLabel pictureView;
private DefaultListModel<String> listModel;
public ListViewPictureViewExample() {
super("ListView PictureView Example");
listModel = new DefaultListModel<>();
listModel.addElement("111");
listModel.addElement("333");
listModel.addElement("444");
listModel.addElement("555");
listView = new JList<>(listModel);
listView.setDragEnabled(true);
listView.setDropMode(DropMode.INSERT);
listView.setTransferHandler(new ListTransferHandler());
pictureView = new JLabel();
pictureView.setTransferHandler(new PictureTransferHandler());
new DropTarget(pictureView, DnDConstants.ACTION_COPY_OR_MOVE, this);
JPanel panel = new JPanel(new GridLayout(1, 2));
panel.add(new JScrollPane(listView));
panel.add(pictureView);
setContentPane(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new ListViewPictureViewExample();
}
@Override
public void dragGestureRecognized(DragGestureEvent event) {
String selectedValue = listView.getSelectedValue();
if (selectedValue != null) {
Transferable transferable = new StringSelection(selectedValue);
event.startDrag(null, transferable);
}
}
@Override
public void dragEnter(DropTargetDragEvent event) {
event.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
}
@Override
public void dragOver(DropTargetDragEvent event) {
event.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
}
@Override
public void drop(DropTargetDropEvent event) {
try {
Transferable transferable = event.getTransferable();
if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
int index = listView.getSelectedIndex();
if (index == -1) {
index = listModel.getSize();
}
listModel.add(index, data);
event.dropComplete(true);
} else {
event.rejectDrop();
}
} catch (Exception e) {
e.printStackTrace();
event.rejectDrop();
}
}
@Override
public void dragExit(DropTargetEvent event) {
}
@Override
public void dropActionChanged(DropTargetDragEvent event) {
}
private class ListTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return MOVE;
}
@Override
protected Transferable createTransferable(JComponent c) {
JList<String> list = (JList<String>) c;
String selectedValue = list.getSelectedValue();
return new StringSelection(selectedValue);
}
@Override
protected void exportDone(JComponent c, Transferable data, int action) {
if (action == MOVE) {
JList<String> list = (JList<String>) c;
int index = list.getSelectedIndex();
listModel.remove(index);
}
}
}
private class PictureTransferHandler extends TransferHandler {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
try {
Transferable transferable = support.getTransferable();
if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
pictureView.setText(data);
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
}
```
这个示例程序创建了一个包含一个 JList 和一个 JLabel 的窗口。您可以从 JList 中拖动一个项目并将其放置在 JLabel 中。您可以使用拖动操作重新排列 JList 中的项目。
阅读全文