本文介紹了使用 TransferHandler 拖動(dòng) JLabel(拖放)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時(shí)送ChatGPT賬號(hào)..
我正在使用 TransferHandler 將數(shù)據(jù)從 JPanel 傳遞到 JTextArea 作為 JLabel(單擊左側(cè)面板中的某處以創(chuàng)建要拖動(dòng)的 JLabel)
I am using a TransferHandler to pass data from a JPanel to a JTextArea as a JLabel (Click somewhere in the left panel to create the JLabel to drag)
數(shù)據(jù)傳輸工作正常,但我還想顯示"JLabel,因?yàn)樗c鼠標(biāo)指針一起被拖動(dòng).
The transfer of the data works fine, but I'd like to also "show" the JLabel as its being dragged along with the mouse pointer.
如果你注釋掉
dropLabel.setTransferHandler(new TransferHandler("text"));
dropLabel.getTransferHandler().exportAsDrag(dropLabel, e,
TransferHandler.COPY);
你會(huì)看到我想要的樣子.(但當(dāng)然數(shù)據(jù)不會(huì)被傳輸).
you will see how I want it to look. (but of course then the data won't be transferred).
如何讓傳輸工作和 JLabel 跟隨鼠標(biāo)光標(biāo)?
How can I get both the transfer to work and the JLabel to follow the mouse cursor?
代碼如下:
import java.awt.*;
import java.awt.datatransfer.Transferable;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
public class DragTest extends JFrame implements MouseMotionListener,
MouseListener {
private JPanel leftPanel = new JPanel(null);
private JPanel rightPanel = new JPanel(null);
private JLabel dragLabel = new JLabel("drop");
private final JWindow window = new JWindow();
JLabel dropLabel;
public DragTest() {
this.setLayout(new GridLayout(1, 2));
leftPanel.setBorder(BorderFactory.createLineBorder(Color.black));
rightPanel.setBorder(BorderFactory.createLineBorder(Color.black));
this.add(leftPanel);
this.add(rightPanel);
leftPanel.addMouseListener(this);
leftPanel.addMouseMotionListener(this);
JTextArea area = new JTextArea();
rightPanel.setLayout(new GridLayout(1, 1));
rightPanel.add(area);
dragLabel.setFont(new Font("Serif", Font.BOLD, 48));
}
@Override
public void mousePressed(MouseEvent e) {
dropLabel = new JLabel("drop");
Dimension labelSize = dropLabel.getPreferredSize();
dropLabel.setSize(labelSize);
int x = e.getX() - labelSize.width / 2;
int y = e.getY() - labelSize.height / 2;
dropLabel.setLocation(x, y);
leftPanel.add(dropLabel);
dropLabel.setTransferHandler(new TransferHandler("text"));
dropLabel.getTransferHandler().exportAsDrag(dropLabel, e,
TransferHandler.COPY);
repaint();
}
@Override
public void mouseDragged(MouseEvent me) {
dragLabel = new JLabel("drop");
dragLabel.setFont(new Font("Serif", Font.BOLD, 48));
int x = me.getPoint().x;
int y = me.getPoint().y;
window.add(dragLabel);
window.pack();
Point pt = new Point(x, y);
Component c = (Component) me.getSource();
SwingUtilities.convertPointToScreen(pt, c);
window.setLocation(pt);
window.setVisible(true);
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
// leftPanel.remove(dropLabel);
window.remove(dragLabel);
window.setVisible(false);
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
public static void main(String[] args) {
DragTest frame = new DragTest();
frame.setVisible(true);
frame.setSize(600, 400);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
推薦答案
再舉一個(gè)例子:
修復(fù)閃爍的光標(biāo)
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.activation.*;
import javax.swing.*;
import javax.swing.text.*;
public class DragTest3 {
public JComponent makeUI() {
DragPanel p1 = new DragPanel();
p1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
p1.add(new JLabel(UIManager.getIcon("OptionPane.warningIcon")));
p1.add(new JLabel(UIManager.getIcon("OptionPane.errorIcon")));
p1.add(new JLabel("Label1"));
p1.add(new JLabel("Label2"));
MouseListener handler = new Handler();
p1.addMouseListener(handler);
LabelTransferHandler th = new LabelTransferHandler();
p1.setTransferHandler(th);
JPanel p = new JPanel(new GridLayout(1,2));
p.add(p1);
DragPanel p2 = new DragPanel();
p2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
p2.addMouseListener(handler);
p2.setTransferHandler(th);
p.add(p2);
JPanel panel = new JPanel(new GridLayout(2,1));
panel.add(p);
panel.add(new JScrollPane(new JTextArea()));
return panel;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new DragTest3().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class DragPanel extends JPanel {
public DragPanel() {
super();
}
public JLabel draggingLabel;
}
class Handler extends MouseAdapter {
@Override public void mousePressed(MouseEvent e) {
DragPanel p = (DragPanel)e.getSource();
Component c = SwingUtilities.getDeepestComponentAt(p, e.getX(), e.getY());
if(c!=null && c instanceof JLabel) {
p.draggingLabel = (JLabel)c;
p.getTransferHandler().exportAsDrag(p, e, TransferHandler.MOVE);
}
}
}
class LabelTransferHandler extends TransferHandler {
private final DataFlavor localObjectFlavor;
private final JLabel label = new JLabel() {
@Override public boolean contains(int x, int y) {
return false;
}
};
private final JWindow window = new JWindow();
public LabelTransferHandler() {
System.out.println("LabelTransferHandler");
localObjectFlavor = new ActivationDataFlavor(
DragPanel.class, DataFlavor.javaJVMLocalObjectMimeType, "JLabel");
window.add(label);
window.setAlwaysOnTop(true);
window.setBackground(new Color(0,true));
DragSource.getDefaultDragSource().addDragSourceMotionListener(
new DragSourceMotionListener() {
@Override public void dragMouseMoved(DragSourceDragEvent dsde) {
Point pt = dsde.getLocation();
pt.translate(5, 5); // offset
window.setLocation(pt);
}
});
}
@Override protected Transferable createTransferable(JComponent c) {
System.out.println("createTransferable");
DragPanel p = (DragPanel)c;
JLabel l = p.draggingLabel;
String text = l.getText();
//TEST
//if(text==null) {
// text = l.getIcon().toString();
/
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!