gpt4 book ai didi

java - JLabel 或 JPanel 的 createTransferable 不会触发

转载 作者:行者123 更新时间:2023-11-30 03:20:54 26 4
gpt4 key购买 nike

缩减了我发现用于测试的样本 JPanelJLabel

新的TransferHandlercreateTransferable() 创建对象要求两者JPanelJLabel

因为没有setDraggable方法,所以我包括了 exportAsDrag (如果对象没有 setDraggable 方法,我认为这是必需的)。

虽然createTransferable( ) 返回null并且不会真正做任何事情我至少应该得到 println已执行,但代码似乎没有进入该部分,这意味着面板或标签都不被视为可拖动对象。

要使其被视为可拖动对象,缺少什么步骤?如果您有一个带有一堆标签的面板,是否建议使面板可拖动并找出哪个标签或使每个单独的标签可拖动?

import java.awt.*;
import java.awt.datatransfer.Transferable;
import javax.swing.*;
import java.awt.event.InputEvent;

public class DnDTransferableTest {

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

public DnDTransferableTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private JLabel label1;
private JLabel label2;

public TestPane() {

setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;
label1 = new JLabel("Drag Me.");
add(label1, gbc);

label2 = new JLabel("Drag Me too.");
gbc.gridx++;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.NONE;
add(label2, gbc);

setTransferHandler(new TransferHandler()
{
@Override
public Transferable createTransferable(final JComponent c)
{
System.out.println("Inside Panel : createTransferable");
return null;
}

@Override
public int getSourceActions(final JComponent c)
{
System.out.println("Inside Panel : getSourceActions()");
return COPY;
}

@Override
public void exportAsDrag(final JComponent comp, final InputEvent e, final int action)
{
System.out.println("Inside Panel : getSourceActions()");
super.exportAsDrag(comp, e, action);
}
});

label1.setTransferHandler(new TransferHandler()
{
@Override
public Transferable createTransferable(final JComponent c)
{
System.out.println("Inside Label : createTransferable");
return null;
}

@Override
public int getSourceActions(final JComponent c)
{
System.out.println("Inside Label : getSourceActions()");
return COPY;
}

@Override
public void exportAsDrag(final JComponent comp, final InputEvent e, final int action)
{
System.out.println("Inside Label : getSourceActions()");
super.exportAsDrag(comp, e, action);
}
});

}
}
}

最佳答案

我认为您需要一个 MouseListener 来允许 JLabel 接受拖动手势,因为它没有 setDragEnabled(...) 方法。

要将 MouseListener 与 DnD 一起使用:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.TransferHandler;

public class DnDTransferableTest {

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

public DnDTransferableTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private JLabel label3;
private JTextField textField = new JTextField(15);

public TestPane() {
label3 = new JLabel("Fubars Rule!");
add(label3);
add(Box.createHorizontalStrut(35));
add(textField);

label3.setTransferHandler(new TransferHandler("text"));
label3.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent mEvt) {
JComponent component = (JComponent) mEvt.getSource();
TransferHandler tHandler = component.getTransferHandler();
tHandler.exportAsDrag(component, mEvt, TransferHandler.COPY);
}
});
}
}
}

关于java - JLabel 或 JPanel 的 createTransferable 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31363563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com