我是 Swing 中拖放的新手。我有一个JPanel
可以绘制带有叠加在其上的标题的图像。我想在这个上实现拖放JPanel
,但是在浏览了一些文档和教程之后,我没有找到任何关于如何为这种类型的组件完成的可用指针。对于初学者来说,它没有setDragEnabled
功能。
我可以做一个JPanel
可拖动的吗?我想使用这个 DnD 操作将某个对象的引用从一个面板传递到另一个面板。
您可以在任何JComponent
. 见setTransferHandler
方法。
该setDragEnabled
方法通常在可以在 JDK 中实现良好默认 D&D 行为的组件上提供。在这种情况下,您可以通过调用该方法来激活默认的 D&D。
JPanel
他们(= Swing 开发人员)可能想不出任何像样的默认 D&D 行为,因此您必须实现自己的TransferHandler
. 我强烈建议在开始之前阅读拖放教程
可能这可以帮助你。
我不知道听起来有多可行,但是当我需要拖放面板时,我是这样做的:
Firstable 我为可拖动的面板和容器实现了动作事件,它既可以是
我为选定的父级、选定的子级和当前面板使用了静态变量
当鼠标悬停在面板上时,您将其设置为当前面板
当您单击时,鼠标按下,无论如何,您检查 currentpanel 是否是被点击的面板并设置为子面板
当鼠标在面板上且子面板不为空时,似乎您正在拖动,一旦您释放鼠标,当前面板将变为父面板
你必须添加一些验证。
如果正在拖动面板,您可以使用自己的实现,它可以跟随鼠标坐标或仅突出显示它并突出显示父级,我使用最后一个选项来模拟拖动
好的,我写了这个,太有问题了,但这就是想法:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JTextField;
/**
*
* @author porfiriopartida
*/
public class DraggablePanel extends JDesktopPane implements ContainerPanel{
public ContainerPanel parent;
static DraggablePanel over;
static ContainerPanel overParent;
static DraggablePanel dragging;
static ContainerPanel draggingParent;
public DraggablePanel(){
this(null);
}
public DraggablePanel(ContainerPanel parent){
this.parent = parent;
setBorder(BorderFactory.createLineBorder(Color.black));
setBounds(0,0,100,100);
if(parent != null)
addMouseListener(new MouseAdapter(){
@Override
public void mouseEntered(MouseEvent me) {
DraggablePanel src = (DraggablePanel) me.getSource();
DraggablePanel.over = src;
DraggablePanel.overParent = DraggablePanel.over.parent;
}
@Override
public void mouseExited(MouseEvent me) {
}
@Override
public void mouseReleased(MouseEvent me) {
if(DraggablePanel.over != null && DraggablePanel.dragging != null && DraggablePanel.overParent != null){
Rectangle bounds = DraggablePanel.dragging.getBounds();
bounds.x = me.getX();
bounds.y = me.getY();
//Remove child from parent
DraggablePanel.dragging.parent.removePanel(DraggablePanel.dragging);
if(DraggablePanel.dragging.parent != DraggablePanel.overParent){
//add child to new parent
DraggablePanel.overParent.addPanel(DraggablePanel.dragging, bounds);
}
else{
//same parent selected
DraggablePanel.dragging.parent.addPanel(DraggablePanel.dragging, bounds);
};
DraggablePanel.dragging.parent = DraggablePanel.overParent;
}
//cleaning variables
DraggablePanel.dragging = null;
DraggablePanel.over = null;
DraggablePanel.draggingParent = null;
DraggablePanel.overParent = null;
}
@Override
public void mousePressed(MouseEvent me) {
DraggablePanel.dragging = (DraggablePanel) me.getSource();
DraggablePanel.draggingParent = DraggablePanel.dragging.parent;
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent me) {
super.mouseEntered(me);
ContainerPanel src = (ContainerPanel) me.getSource();
DraggablePanel.overParent = src;
if (DraggablePanel.draggingParent == null || DraggablePanel.draggingParent == src) {
setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
} else {
setBorder(BorderFactory.createLineBorder(Color.blue, 2));
}
}
@Override
public void mouseExited(MouseEvent me) {
}
@Override
public void mouseReleased(MouseEvent me) {
}
});
}
@Override
public void addPanel(DraggablePanel panel) {
panel.parent = this;
add(panel);
repaint();
revalidate();
try {
getParent().repaint();
} catch (Exception e) {
}
}
@Override
public void addPanel(DraggablePanel panel, Rectangle bounds) {
setBounds(bounds);
addPanel(panel);
}
@Override
public void removePanel(DraggablePanel panel) {
remove(panel);
}
public static void main(String args[]){
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1,1));
JTextField tf = new JTextField("textfield");
JButton btn = new JButton("Button");
DraggablePanel desktop = new DraggablePanel();
frame.add(desktop);
DraggablePanel p1 = new DraggablePanel(desktop);
p1.setLayout(new GridLayout(2,1));
p1.add(tf);
p1.setBounds(0,0,100,50);
tf.setBounds(5,5,80,30);
DraggablePanel p2 = new DraggablePanel(desktop);
p2.setLayout(new GridLayout(2,1));
p2.add(btn);
p2.setBounds(50,50,50,30);
btn.setBounds(5,5,30,20);
desktop.add(p1);
desktop.add(p2);
frame.setPreferredSize(new Dimension(600,400));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}