2

这是我的第一个问题,请耐心等待。假设我有 2 个 JPanel 和 1 个 JToolBar。我想要做的是将工具栏从一个面板拖动到另一个面板,并且在释放鼠标后,工具栏应该粘在第二个面板上。

4

1 回答 1

3

BasicToolbarUI has floatAt method. As you can see from source (below) toolbar uses docing source which is toolbar's parent container. You can try to override the method and replace the source.

   protected void floatAt(Point position, Point origin)
    {
    if(toolBar.isFloatable() == true)
    {
      try
      {
        Point offset = dragWindow.getOffset();
        if (offset == null) {
        offset = position;
        dragWindow.setOffset(offset);
        }
        Point global = new Point(origin.x+ position.x,
                     origin.y+position.y);
        setFloatingLocation(global.x-offset.x, 
                global.y-offset.y);
        if (dockingSource != null) { 
        Point dockingPosition = dockingSource.getLocationOnScreen();
        Point comparisonPoint = new Point(global.x-dockingPosition.x,
                          global.y-dockingPosition.y);
        if (canDock(dockingSource, comparisonPoint)) {
            setFloating(false, comparisonPoint);
        } else {
            setFloating(true, null);
        }
        } else {
        setFloating(true, null);
        }
于 2011-11-25T10:51:25.450 回答