【问题标题】:Drag undecorated jDialog拖动未修饰的 jDialog
【发布时间】:2013-03-29 19:49:06
【问题描述】:

所以,我有一个 jFrame,我正在其中构建聊天的主界面窗口。这个窗口/jFrame 有几个按钮,每个按钮都显示一个 jDialog(我之前在 Netbeans 中创建它,将 jDialog 拖到父级(?) jFrame 上)。

我的问题是两个窗口都设置为undecorated = true,所以我希望让用户通过单击并拖动窗口的一部分来随意拖动和移动所有窗口(在未装饰时模拟标题栏)

在所有 jFrames 中,我在 initComponents() 之后通过以下代码完成了此操作:

    final Point point = new Point(0,0);        // Why 'final' and not simply Point point?     
    addMouseListener(new MouseAdapter() {  
        public void mousePressed(MouseEvent e) {  
            if(!e.isMetaDown()){  
                point.x = e.getX();  
                point.y = e.getY(); 
                System.out.println("Ratón pulsado: " + point.x + "," + point.y);
            }  
        }  
    });

    addMouseMotionListener(new MouseMotionAdapter() {  
        public void mouseDragged(MouseEvent e) {  
            if(!e.isMetaDown() &&  point.y <= 17){  //Coordinates of title bar, any X and up to 17px from the top border
                Point p = getLocation();  
                setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y); 
                System.out.println("Ratón movido: " + (p.x + e.getX() - point.x) + "," + (p.y + e.getY() - point.y));
            }  
        }  
    }); 

但是,我不知道如何在 jDialog 中使用此代码。当我在导航器中右键单击它并选择自定义代码时,我无法将其粘贴到那里,因为整个 jFrame 停止工作。我是 jFrames 的 jDialogs 子项的新手,所以请帮助我一些指导方针:) 谢谢

【问题讨论】:

  • 搜索组件移动器,别忘了给这个(那些)答案的作者点赞
  • 对于example。

标签: java swing jframe draggable jdialog


【解决方案1】:

好吧,正如@mKorbel 建议的那样,我前往here,在那里我找到了一个名为ComponentMover 的好课程,它帮助我做到了这一点。我还需要 2 点声望,所以我保存了链接,以便在我能够做到时返回并投票。

我必须确保它以我想要的方式完美运行,但看起来很棒!谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-20
    • 2013-06-29
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多