设置JLabel为可被拉拽复制对象(DragDrop)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingDragDrop {
	JTextField txtField;
	JLabel lbl;

	public static void main(String[] args) {
		new SwingDragDrop();
	}

	public SwingDragDrop() {
		JFrame frame = new JFrame("Drag Drop Demo");
		txtField = new JTextField(20);
		lbl = new JLabel("This is the text for drag and drop.");
		lbl.setTransferHandler(new TransferHandler("text"));// 设置为可被拉拽
		MouseListener ml = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				JComponent jc = (JComponent) e.getSource();
				TransferHandler th = jc.getTransferHandler();
				th.exportAsDrag(jc, e, TransferHandler.COPY);
			}
		};
		lbl.addMouseListener(ml);
		JPanel panel = new JPanel();
		panel.add(txtField);
		frame.add(lbl, BorderLayout.CENTER);
		frame.add(panel, BorderLayout.NORTH);
		frame.setSize(400, 400);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
	}
}

 

你可能感兴趣的:(java,swing)