将jtoolbar从一个jpanel转移到另一个 [英] Transfer jtoolbar from one jpanel to another

查看:137
本文介绍了将jtoolbar从一个jpanel转移到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题,请多多包涵.假设我有2个JPanel和1个JToolBar.我要做的是将工具栏从一个面板拖到另一个面板,放开鼠标后,工具栏应停留在第二个面板上.

This is my first question, so bear with me. Let's say that I have 2 JPanel and 1 JToolBar. What I want to do is drag the toolbar from one panel to another and, after mouse release, the toolbar should stick to the second.

推荐答案

BasicToolbarUI具有floatAt方法.如您从源代码中看到的(在下面),工具栏使用了docing源,它是工具栏的父容器.您可以尝试覆盖该方法并替换源.

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);
        }

这篇关于将jtoolbar从一个jpanel转移到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆