单击JPanel移动未修饰的窗口 [英] Moving undecorated window by clicking on JPanel

查看:111
本文介绍了单击JPanel移动未修饰的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当该窗口未修饰时,是否有可能通过单击窗口中的一个面板来移动窗口?

Is there a possibility to move window by clicking on one of the panels in the window when that window is undecorated?

我有一个带有遮罩边框的主面板40像素大小,内部控件的面板很少,我想在点击边框时移动窗口。这可能吗?

I have a main panel with matte border 40 pixels size, and few panels with controls inside, and I would like to move the window when clicking on that border. Is that possible?

推荐答案

你可以在带有边框的面板上放置另一个面板,边框可见。使用以下代码移动窗口。

You can place another panel over the panel with the border, leaving the border visible.Use the following code to move your window.

public class MotionPanel extends JPanel{
    private Point initialClick;
    private JFrame parent;

    public MotionPanel(final JFrame parent){
    this.parent = parent;

    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            initialClick = e.getPoint();
            getComponentAt(initialClick);
        }
    });

    addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {

            // get location of Window
            int thisX = parent.getLocation().x;
            int thisY = parent.getLocation().y;

            // Determine how much the mouse moved since the initial click
            int xMoved = e.getX() - initialClick.x;
            int yMoved = e.getY() - initialClick.y;

            // Move window to this position
            int X = thisX + xMoved;
            int Y = thisY + yMoved;
            parent.setLocation(X, Y);
        }
    });
    }
}

我一直在使用此代码一段时间现在为未修饰的窗口制作自定义标题栏。
P.S.:你可以通过扩展JComponent而不是JPanel来概括这个例子。

I've been working with this code for a while now to make a custom titlebar for undecorated windows. P.S.:You can generalize this example by extending JComponent instead of JPanel.

这篇关于单击JPanel移动未修饰的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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