Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将JTabbedpane拖出窗口并自动创建新JFrame_Java_Swing_Drag And Drop_Jtabbedpane - Fatal编程技术网

Java 将JTabbedpane拖出窗口并自动创建新JFrame

Java 将JTabbedpane拖出窗口并自动创建新JFrame,java,swing,drag-and-drop,jtabbedpane,Java,Swing,Drag And Drop,Jtabbedpane,早上好 首先,我的任务是让我的JTabbedPane拖出窗口并创建新的JFrame。插图类似于NetBeans/SublimiteText 我在中找到了拖放JTabbedPane的参考,从1个JTabbedPane或2/多个JTabbedPane拖放选项卡是成功的 现在我想要的是,每当拖出文档的tab时,它都会自动创建新的框架,并将tab插入到新的jtabbedPane中。如果jtabbedpane为空/无组件,则jframe将自动关闭 -- “简短问题” 当我将tab(从jtabbedpane

早上好

首先,我的任务是让我的JTabbedPane拖出窗口并创建新的JFrame。插图类似于NetBeans/SublimiteText

我在中找到了拖放JTabbedPane的参考,从1个JTabbedPane或2/多个JTabbedPane拖放选项卡是成功的

现在我想要的是,每当拖出文档的tab时,它都会自动创建新的框架,并将tab插入到新的jtabbedPane中。如果jtabbedpane为空/无组件,则jframe将自动关闭

--

“简短问题” 当我将tab(从jtabbedpane)拖动到桌面/JFrame外部,并在内部自动创建JFrame和Tabbedpane,然后将选项卡放在那里时,我是否可以

另外,当选项卡拖放并且jtabbedpane中没有组件时,它可以自动处理JFrame/jtabbedpane

“非常简短的问题” 如何获得类似JTabbedPane的Netbeans选项卡窗格文档编辑器

--

我使用“关闭”按钮包括我最新修改的拖放JTabbedPane文件

多谢各位。R

public class DnDCloseButtonTabbedPane extends JTabbedPane {

    public static final long serialVersionUID = 1L;
    private static final int LINEWIDTH = 3;
    private static final String NAME = "TabTransferData";
    private final DataFlavor FLAVOR = new DataFlavor(
            DataFlavor.javaJVMLocalObjectMimeType, NAME);
    private static GhostGlassPane s_glassPane = new GhostGlassPane();

    private boolean m_isDrawRect = false;
    private final Rectangle2D m_lineRect = new Rectangle2D.Double();

    private final Color m_lineColor = new Color(0, 100, 255);
    private TabAcceptor m_acceptor = null;

    private final DropTarget dropTarget;

    private final ImageIcon icon;
    private final Dimension buttonSize;

    public DnDCloseButtonTabbedPane() {
        super();
        final DragSourceListener dsl = new DragSourceListener() {
            @Override
            public void dragEnter(DragSourceDragEvent e) {
                e.getDragSourceContext().setCursor(DragSource.DefaultMoveDrop);
            }

            @Override
            public void dragExit(DragSourceEvent e) {
                e.getDragSourceContext()
                        .setCursor(DragSource.DefaultMoveNoDrop);
                m_lineRect.setRect(0, 0, 0, 0);
                m_isDrawRect = false;
                s_glassPane.setPoint(new Point(-1000, -1000));
                s_glassPane.repaint();
//                System.out.println(e);
            }

            @Override
            public void dragOver(DragSourceDragEvent e) {
                //e.getLocation()
                //This method returns a Point indicating the cursor location in screen coordinates at the moment

                TabTransferData data = getTabTransferData(e);

                if (data == null) {
                    e.getDragSourceContext().setCursor(
                            DragSource.DefaultMoveNoDrop);
                    return;
                } // if

                /*
                 Point tabPt = e.getLocation();
                 SwingUtilities.convertPointFromScreen(tabPt, DnDTabbedPane.this);
                 if (DnDTabbedPane.this.contains(tabPt)) {
                 int targetIdx = getTargetTabIndex(tabPt);
                 int sourceIndex = data.getTabIndex();
                 if (getTabAreaBound().contains(tabPt)
                 && (targetIdx >= 0)
                 && (targetIdx != sourceIndex)
                 && (targetIdx != sourceIndex + 1)) {
                 e.getDragSourceContext().setCursor(
                 DragSource.DefaultMoveDrop);

                 return;
                 } // if

                 e.getDragSourceContext().setCursor(
                 DragSource.DefaultMoveNoDrop);
                 return;
                 } // if
                 */
                e.getDragSourceContext().setCursor(
                        DragSource.DefaultMoveDrop);
            }

            public void dragDropEnd(DragSourceDropEvent e) {
                m_isDrawRect = false;
                m_lineRect.setRect(0, 0, 0, 0);
                // m_dragTabIndex = -1;

                if (hasGhost()) {
                    s_glassPane.setVisible(false);
                    s_glassPane.setImage(null);
                }

            }

            @Override
            public void dropActionChanged(DragSourceDragEvent e) {

            }
        };

        final DragGestureListener dgl = new DragGestureListener() {
            @Override
            public void dragGestureRecognized(DragGestureEvent e) {
                // System.out.println("dragGestureRecognized");

                Point tabPt = e.getDragOrigin();
                int dragTabIndex = indexAtLocation(tabPt.x, tabPt.y);
                if (dragTabIndex < 0) {
                    return;
                } // if

                initGlassPane(e.getComponent(), e.getDragOrigin(), dragTabIndex);
                try {
                    e.startDrag(DragSource.DefaultMoveDrop,
                            new TabTransferable(DnDCloseButtonTabbedPane.this, dragTabIndex), dsl);
                } catch (InvalidDnDOperationException idoe) {
                    idoe.printStackTrace();
                }
            }
        };

        //dropTarget =
        dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE,
                new CDropTargetListener(), true);
        new DragSource().createDefaultDragGestureRecognizer(this,
                DnDConstants.ACTION_COPY_OR_MOVE, dgl);
        m_acceptor = new TabAcceptor() {
            @Override
            public boolean isDropAcceptable(DnDCloseButtonTabbedPane a_component, int a_index) {
                return true;
            }
        };

        icon = new ImageIcon(getClass().getResource(Resource.ICON_16X16 + "delete.png"));
        buttonSize = new Dimension(icon.getIconWidth(), icon.getIconHeight());
    }

    @Override
    public void addTab(String title, final Component component) {
        JPanel tab = new JPanel(new BorderLayout());
        tab.setOpaque(false);
        JLabel label = new JLabel(title);
        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 4));
        JButton button = new JButton(icon);
        button.setPreferredSize(buttonSize);
        button.setUI(new BasicButtonUI());
        button.setContentAreaFilled(false);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((DnDCloseButtonTabbedPane) component.getParent()).remove(component);
            }
        });
        tab.add(label, BorderLayout.WEST);
        tab.add(button, BorderLayout.EAST);
        tab.setBorder(BorderFactory.createEmptyBorder(2, 1, 1, 1));
        super.addTab(title, component);
        setTabComponentAt(indexOfComponent(component), tab);
    }

    public TabAcceptor getAcceptor() {
        return m_acceptor;
    }

    public void setAcceptor(TabAcceptor a_value) {
        m_acceptor = a_value;
    }

    private TabTransferData getTabTransferData(DropTargetDropEvent a_event) {
        try {
            TabTransferData data = (TabTransferData) a_event.getTransferable().getTransferData(FLAVOR);
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private TabTransferData getTabTransferData(DropTargetDragEvent a_event) {
        try {
            TabTransferData data = (TabTransferData) a_event.getTransferable().getTransferData(FLAVOR);
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private TabTransferData getTabTransferData(DragSourceDragEvent a_event) {
        try {
            TabTransferData data = (TabTransferData) a_event.getDragSourceContext()
                    .getTransferable().getTransferData(FLAVOR);
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private TabTransferData getTabTransferData(DragSourceDropEvent a_event) {
        try {
            TabTransferData data = (TabTransferData) a_event.getDragSourceContext()
                    .getTransferable().getTransferData(FLAVOR);
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    class TabTransferable implements Transferable {

        private TabTransferData m_data = null;

        public TabTransferable(DnDCloseButtonTabbedPane a_tabbedPane, int a_tabIndex) {
            m_data = new TabTransferData(DnDCloseButtonTabbedPane.this, a_tabIndex);
        }

        public Object getTransferData(DataFlavor flavor) {
            return m_data;
            // return DnDTabbedPane.this;
        }

        public DataFlavor[] getTransferDataFlavors() {
            DataFlavor[] f = new DataFlavor[1];
            f[0] = FLAVOR;
            return f;
        }

        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return flavor.getHumanPresentableName().equals(NAME);
        }
    }

    class TabTransferData {

        private DnDCloseButtonTabbedPane m_tabbedPane = null;
        private int m_tabIndex = -1;

        public TabTransferData() {
        }

        public TabTransferData(DnDCloseButtonTabbedPane a_tabbedPane, int a_tabIndex) {
            m_tabbedPane = a_tabbedPane;
            m_tabIndex = a_tabIndex;
        }

        public DnDCloseButtonTabbedPane getTabbedPane() {
            return m_tabbedPane;
        }

        public void setTabbedPane(DnDCloseButtonTabbedPane pane) {
            m_tabbedPane = pane;
        }

        public int getTabIndex() {
            return m_tabIndex;
        }

        public void setTabIndex(int index) {
            m_tabIndex = index;
        }
    }

    private Point buildGhostLocation(Point a_location) {
        Point retval = new Point(a_location);

//        switch (getTabPlacement()) {
//            case JTabbedPane.TOP: {
//                retval.y = 1;
//                retval.x -= s_glassPane.getGhostWidth() / 2;
//            }
//            break;
//
//            case JTabbedPane.BOTTOM: {
//                retval.y = getHeight() - 1 - s_glassPane.getGhostHeight();
//                retval.x -= s_glassPane.getGhostWidth() / 2;
//            }
//            break;
//
//            case JTabbedPane.LEFT: {
//                retval.x = 1;
//                retval.y -= s_glassPane.getGhostHeight() / 2;
//            }
//            break;
//
//            case JTabbedPane.RIGHT: {
//                retval.x = getWidth() - 1 - s_glassPane.getGhostWidth();
//                retval.y -= s_glassPane.getGhostHeight() / 2;
//            }
//            break;
//        } // switch
        retval = SwingUtilities.convertPoint(DnDCloseButtonTabbedPane.this,
                retval, s_glassPane);
        return retval;
    }

    class CDropTargetListener implements DropTargetListener {

        public void dragEnter(DropTargetDragEvent e) {
//             System.out.println("DropTarget.dragEnter: " + DnDCloseButtonTabbedPane.this);

            if (isDragAcceptable(e)) {
                e.acceptDrag(e.getDropAction());
            } else {
                e.rejectDrag();
            } // if
        }

        public void dragExit(DropTargetEvent e) {
//            System.out.println("DropTarget.dragExit: " + DnDCloseButtonTabbedPane.this);
            m_isDrawRect = false;
        }

        public void dropActionChanged(DropTargetDragEvent e) {
        }

        public void dragOver(final DropTargetDragEvent e) {
            TabTransferData data = getTabTransferData(e);

            if (getTabPlacement() == JTabbedPane.TOP
                    || getTabPlacement() == JTabbedPane.BOTTOM) {
                initTargetLeftRightLine(getTargetTabIndex(e.getLocation()), data);
            } else {
                initTargetTopBottomLine(getTargetTabIndex(e.getLocation()), data);
            } // if-else

            repaint();
            if (hasGhost()) {
                s_glassPane.setPoint(buildGhostLocation(e.getLocation()));
                s_glassPane.repaint();
            }
        }

        @Override
        public void drop(DropTargetDropEvent a_event) {
//             System.out.println("DropTarget.drop: " + DnDTabbedPane.this);

            if (isDropAcceptable(a_event)) {
                convertTab(getTabTransferData(a_event),
                        getTargetTabIndex(a_event.getLocation()));
                a_event.dropComplete(true);
            } else {
                a_event.dropComplete(false);
            } // if-else

            m_isDrawRect = false;
            repaint();
        }

        public boolean isDragAcceptable(DropTargetDragEvent e) {
            Transferable t = e.getTransferable();
            if (t == null) {
                return false;
            } // if

            DataFlavor[] flavor = e.getCurrentDataFlavors();
            if (!t.isDataFlavorSupported(flavor[0])) {
                return false;
            } // if

            TabTransferData data = getTabTransferData(e);

            if (DnDCloseButtonTabbedPane.this == data.getTabbedPane()
                    && data.getTabIndex() >= 0) {
                return true;
            } // if

            if (DnDCloseButtonTabbedPane.this != data.getTabbedPane()) {
                if (m_acceptor != null) {
                    return m_acceptor.isDropAcceptable(data.getTabbedPane(), data.getTabIndex());
                } // if
            } // if

            boolean transferDataFlavorFound = false;
            for (DataFlavor transferDataFlavor : t.getTransferDataFlavors()) {
                if (FLAVOR.equals(transferDataFlavor)) {
                    transferDataFlavorFound = true;
                    break;
                }
            }
            if (transferDataFlavorFound == false) {
                return false;
            }
            return false;
        }

        public boolean isDropAcceptable(DropTargetDropEvent e) {

            Transferable t = e.getTransferable();
            if (t == null) {
                return false;
            } // if

            DataFlavor[] flavor = e.getCurrentDataFlavors();
            if (!t.isDataFlavorSupported(flavor[0])) {
                return false;
            } // if

            TabTransferData data = getTabTransferData(e);

            if (DnDCloseButtonTabbedPane.this == data.getTabbedPane()
                    && data.getTabIndex() >= 0) {
                return true;
            } // if

            if (DnDCloseButtonTabbedPane.this != data.getTabbedPane()) {
                if (m_acceptor != null) {
                    return m_acceptor.isDropAcceptable(data.getTabbedPane(), data.getTabIndex());
                } // if
            } // if

            return false;
        }
    }

    private boolean m_hasGhost = true;

    public void setPaintGhost(boolean flag) {
        m_hasGhost = flag;
    }

    public boolean hasGhost() {
        return m_hasGhost;
    }

    /**
     * returns potential index for drop.
     *
     * @param a_point point given in the drop site component's coordinate
     * @return returns potential index for drop.
     */
    private int getTargetTabIndex(Point a_point) {
        boolean isTopOrBottom = getTabPlacement() == JTabbedPane.TOP
                || getTabPlacement() == JTabbedPane.BOTTOM;

        // if the pane is empty, the target index is always zero.
        if (getTabCount() == 0) {
            return 0;
        } // if

        for (int i = 0; i < getTabCount(); i++) {
            Rectangle r = getBoundsAt(i);
            if (isTopOrBottom) {
                r.setRect(r.x - r.width / 2, r.y, r.width, r.height);
            } else {
                r.setRect(r.x, r.y - r.height / 2, r.width, r.height);
            } // if-else

            if (r.contains(a_point)) {
                return i;
            } // if
        } // for

        Rectangle r = getBoundsAt(getTabCount() - 1);
        if (isTopOrBottom) {
            int x = r.x + r.width / 2;
            r.setRect(x, r.y, getWidth() - x, r.height);
        } else {
            int y = r.y + r.height / 2;
            r.setRect(r.x, y, r.width, getHeight() - y);
        } // if-else

        return r.contains(a_point) ? getTabCount() : -1;
    }

    private void convertTab(TabTransferData a_data, int a_targetIndex) {
        DnDCloseButtonTabbedPane source = a_data.getTabbedPane();
//        System.out.println("this=source? " + (this == source));
        int sourceIndex = a_data.getTabIndex();
        if (sourceIndex < 0) {
            return;
        } // if
        //Save the tab's component, title, and TabComponent.
        Component cmp = source.getComponentAt(sourceIndex);
        String str = source.getTitleAt(sourceIndex);
        Component tcmp = source.getTabComponentAt(sourceIndex);

        if (this != source) {
            source.remove(sourceIndex);

            if (a_targetIndex == getTabCount()) {
                addTab(str, cmp);
                setTabComponentAt(getTabCount() - 1, tcmp);
            } else {
                if (a_targetIndex < 0) {
                    a_targetIndex = 0;
                } // if

                insertTab(str, null, cmp, null, a_targetIndex);
                setTabComponentAt(a_targetIndex, tcmp);
            } // if

            setSelectedComponent(cmp);
            return;
        } // if
        if (a_targetIndex < 0 || sourceIndex == a_targetIndex) {
            return;
        } // if
        if (a_targetIndex == getTabCount()) {
            source.remove(sourceIndex);
            addTab(str, cmp);
            setTabComponentAt(getTabCount() - 1, tcmp);
            setSelectedIndex(getTabCount() - 1);
        } else if (sourceIndex > a_targetIndex) {
            source.remove(sourceIndex);
            insertTab(str, null, cmp, null, a_targetIndex);
            setTabComponentAt(a_targetIndex, tcmp);
            setSelectedIndex(a_targetIndex);
        } else {
            source.remove(sourceIndex);
            insertTab(str, null, cmp, null, a_targetIndex - 1);
            setTabComponentAt(a_targetIndex - 1, tcmp);
            setSelectedIndex(a_targetIndex - 1);
        }
    }

    private void initTargetLeftRightLine(int next, TabTransferData a_data) {
        if (next < 0) {
            m_lineRect.setRect(0, 0, 0, 0);
            m_isDrawRect = false;
            return;
        } // if

        if ((a_data.getTabbedPane() == this)
                && (a_data.getTabIndex() == next
                || next - a_data.getTabIndex() == 1)) {
            m_lineRect.setRect(0, 0, 0, 0);
            m_isDrawRect = false;
        } else if (getTabCount() == 0) {
            m_lineRect.setRect(0, 0, 0, 0);
            m_isDrawRect = false;
            return;
        } else if (next == 0) {
            Rectangle rect = getBoundsAt(0);
            m_lineRect.setRect(-LINEWIDTH / 2, rect.y, LINEWIDTH, rect.height);
            m_isDrawRect = true;
        } else if (next == getTabCount()) {
            Rectangle rect = getBoundsAt(getTabCount() - 1);
            m_lineRect.setRect(rect.x + rect.width - LINEWIDTH / 2, rect.y,
                    LINEWIDTH, rect.height);
            m_isDrawRect = true;
        } else {
            Rectangle rect = getBoundsAt(next - 1);
            m_lineRect.setRect(rect.x + rect.width - LINEWIDTH / 2, rect.y,
                    LINEWIDTH, rect.height);
            m_isDrawRect = true;
        }
    }

    private void initTargetTopBottomLine(int next, TabTransferData a_data) {
        if (next < 0) {
            m_lineRect.setRect(0, 0, 0, 0);
            m_isDrawRect = false;
            return;
        } // if

        if ((a_data.getTabbedPane() == this)
                && (a_data.getTabIndex() == next
                || next - a_data.getTabIndex() == 1)) {
            m_lineRect.setRect(0, 0, 0, 0);
            m_isDrawRect = false;
        } else if (getTabCount() == 0) {
            m_lineRect.setRect(0, 0, 0, 0);
            m_isDrawRect = false;
            return;
        } else if (next == getTabCount()) {
            Rectangle rect = getBoundsAt(getTabCount() - 1);
            m_lineRect.setRect(rect.x, rect.y + rect.height - LINEWIDTH / 2,
                    rect.width, LINEWIDTH);
            m_isDrawRect = true;
        } else if (next == 0) {
            Rectangle rect = getBoundsAt(0);
            m_lineRect.setRect(rect.x, -LINEWIDTH / 2, rect.width, LINEWIDTH);
            m_isDrawRect = true;
        } else {
            Rectangle rect = getBoundsAt(next - 1);
            m_lineRect.setRect(rect.x, rect.y + rect.height - LINEWIDTH / 2,
                    rect.width, LINEWIDTH);
            m_isDrawRect = true;
        }
    }

    private void initGlassPane(Component c, Point tabPt, int a_tabIndex) {
        //Point p = (Point) pt.clone();
        getRootPane().setGlassPane(s_glassPane);
        if (hasGhost()) {
            Rectangle rect = getBoundsAt(a_tabIndex);
            BufferedImage image = new BufferedImage(c.getWidth(),
                    c.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics g = image.getGraphics();
            c.paint(g);
            image = image.getSubimage(rect.x, rect.y, rect.width, rect.height);
            s_glassPane.setImage(image);
        } // if

        s_glassPane.setPoint(buildGhostLocation(tabPt));
        s_glassPane.setVisible(true);
    }

    private Rectangle getTabAreaBound() {
        Rectangle lastTab = getUI().getTabBounds(this, getTabCount() - 1);
        return new Rectangle(0, 0, getWidth(), lastTab.y + lastTab.height);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (m_isDrawRect) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(m_lineColor);
            g2.fill(m_lineRect);
        } // if
    }

    public interface TabAcceptor {

        boolean isDropAcceptable(DnDCloseButtonTabbedPane a_component, int a_index);
    }
}

class GhostGlassPane extends JPanel {

    public static final long serialVersionUID = 1L;
    private final AlphaComposite m_composite;

    private Point m_location = new Point(0, 0);

    private BufferedImage m_draggingGhost = null;

    public GhostGlassPane() {
        setOpaque(false);
        m_composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f);
    }

    public void setImage(BufferedImage draggingGhost) {
        m_draggingGhost = draggingGhost;
    }

    public void setPoint(Point a_location) {
        m_location.x = a_location.x;
        m_location.y = a_location.y;
    }

    public int getGhostWidth() {
        if (m_draggingGhost == null) {
            return 0;
        } // if

        return m_draggingGhost.getWidth(this);
    }

    public int getGhostHeight() {
        if (m_draggingGhost == null) {
            return 0;
        } // if

        return m_draggingGhost.getHeight(this);
    }

    public void paintComponent(Graphics g) {
        if (m_draggingGhost == null) {
            return;
        } // if 

        Graphics2D g2 = (Graphics2D) g;
        g2.setComposite(m_composite);

        g2.drawImage(m_draggingGhost, (int) m_location.getX(), (int) m_location.getY(), null);
    }
}
公共类DNDCloseButtonAbbedPane扩展了JTabbedPane{
公共静态最终长serialVersionUID=1L;
专用静态最终整型线宽=3;
私有静态最终字符串NAME=“TabTransferData”;
私有最终DataFlavor=新DataFlavor(
DataFlavor.javaJVMLocalObjectMimeType,NAME);
私有静态GhostGlassPane s_glassPane=新GhostGlassPane();
私有布尔m_isDrawRect=false;
私有最终矩形2D m_lineRect=新矩形2D.Double();
私有最终颜色m_lineColor=新颜色(0、100、255);
私有TabAcceptor m_acceptor=null;
私有最终DropTarget DropTarget;
私人最终图像图标;
私有最终尺寸按钮化;
公共DnDCloseButtonTabbedPane(){
超级();
最终DRAGSOURCELISTER dsl=新DRAGSOURCELISTER(){
@凌驾
公共无效拖缆器(拖缆器){
e、 getDragSourceContext().setCursor(DragSource.DefaultMoveDrop);
}
@凌驾
公共无效dragExit(DRAGSOURCEE){
e、 getDragSourceContext()
.setCursor(DragSource.DefaultMoveNoDrop);
m_lineRect.setRect(0,0,0,0);
m_isDrawRect=假;
s_glassPane.设定点(新点(-1000,-1000));
s_glassPane.repaint();
//系统输出打印ln(e);
}
@凌驾
公共无效dragOver(DRAGSOURCEDRAGE){
//e、 getLocation()
//此方法返回一个点,该点指示当前光标在屏幕坐标中的位置
TabTransferData=getTabTransferData(e);
如果(数据==null){
e、 getDragSourceContext().setCursor(
DragSource.DefaultMoveNoDrop);
返回;
}//如果
/*
点tabPt=e.getLocation();
SwingUtilities.convertPointFromScreen(选项卡,DnDTabbedPane.this);
if(DnDTabbedPane.this.contains(tabPt)){
int-targetIdx=getTargetTabIndex(tabPt);
int sourceIndex=data.getTabIndex();
if(gettabreabound().contains)(tabPt)
&&(targetIdx>=0)
&&(targetIdx!=源索引)
&&(targetIdx!=sourceIndex+1)){
e、 getDragSourceContext().setCursor(
DragSource.DefaultMoveDrop);
返回;
}//如果
e、 getDragSourceContext().setCursor(
DragSource.DefaultMoveNoDrop);
返回;
}//如果
*/
e、 getDragSourceContext().setCursor(
DragSource.DefaultMoveDrop);
}
公共无效dragDropEnd(DragSourceDropEvent e){
m_isDrawRect=假;
m_lineRect.setRect(0,0,0,0);
//m_dragTabIndex=-1;
if(hasGhost()){
s_glassPane.setVisible(假);
s_glassPane.setImage(空);
}
}
@凌驾
公共作废dropActionChanged(DRAGSOURCEDRAGE事件){
}
};
最终DragGestureListener dgl=新DragGestureListener(){
@凌驾
公共无效DragestureRecognited(DragestureEvent e){
//System.out.println(“Draggesturererecogned”);
点tabPt=e.getDragOrigin();
int dragTabIndex=指数化位置(表x、表y);
如果(dragTabIndex<0){
返回;
}//如果
initGlassPane(e.getComponent()、e.getDragOrigin()、dragTabIndex);
试一试{
e、 startDrag(DragSource.DefaultMoveDrop,
新标签(DnDCloseButtonTabbedPane.this,dragTabIndex),dsl);
}捕获(无效DNDOperationException idoe){
idoe.printStackTrace();
}
}
};
//投靶=
dropTarget=新的dropTarget(此,DnDConstants.ACTION\u COPY\u或\u MOVE,
新的CDropTargetListener(),true);
新建DragSource().CreateDefaultDraggestureRecogener(此,
DnDConstants.ACTION(复制或移动,dgl);
m_acceptor=新TabAcceptor(){
@凌驾
公共布尔值isDropAcceptable(DnDCloseButtonTabbedPane a_组件,int a_索引){
返回true;
}
};
icon=newImageIcon(getClass().getResource(Resource.icon_16X16+“delete.png”);
buttonSize=新维度(icon.getIconWidth(),icon.getIconHeight());
}
@
        @Override
        public void dragDropEnd(DragSourceDropEvent e) {
            m_isDrawRect = false;
            m_lineRect.setRect(0, 0, 0, 0);
            // m_dragTabIndex = -1;

            if (hasGhost()) {
                s_glassPane.setVisible(false);
                s_glassPane.setImage(null);
            }

            // if drop failed, create new JFrame with JTabbedPane included with public access
            if(!e.getDropSuccess()){
                // MenuLight class Extends JFrame and Included 1 component JTabbedPane called superPane
                MenuLight m = new MenuLight();
                m.setLocation(e.getLocation());
                m.setVisible(true);

                // after create Frame, transfer the tab to other jtabbedpane
                ((DnDCloseButtonTabbedPane) m.superPane).convertTab(getTabTransferData(e), getTargetTabIndex(e.getLocation()));
            }

            // if current JTabbedPane Tab is empty dispose it.
            if(getTabCount() < 1){
                // unfortunly i didnt want to close my Original menu, so check the class of parent of DnD is create from MenuLight and dispose it
                if(parent.getClass().equals(MenuLight.class)){
                    ((javax.swing.JFrame) parent).dispose();
                }
            }
        }
public DnDCloseButtonTabbedPane(final Component _parent)
public class MenuLight extends javax.swing.JFrame {

    public MenuLight() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        superPane = new com.theflavare.minierp.helper.DnDCloseButtonTabbedPane(this);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(640, 480));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(superPane, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(superPane, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    // Variables declaration - do not modify                     
    public javax.swing.JTabbedPane superPane;
    // End of variables declaration                   
}