Java 通过单击JPanel移动未装饰的窗口

Java 通过单击JPanel移动未装饰的窗口,java,swing,border,panel,Java,Swing,Border,Panel,当窗口未装饰时,是否可以通过单击窗口中的一个面板来移动窗口 我有一个40像素大小的无光边框主面板,还有几个内部有控件的面板,我想在点击边框时移动窗口。有可能吗?是的,很有可能。你需要一个鼠标听者来监听鼠标事件。你开始移动鼠标向下,然后停止移动鼠标。然后,您只需按照鼠标在该阶段的平移量平移窗口位置(计算旧鼠标位置和新鼠标位置之间的增量,并将其添加到帧位置)。使用鼠标侦听器应该可以相当轻松地完成此操作 我有一个40像素大小的无光边框主面板,还有几个内部有控件的面板,我想在点击边框时移动窗口 我认为@

当窗口未装饰时,是否可以通过单击窗口中的一个面板来移动窗口


我有一个40像素大小的无光边框主面板,还有几个内部有控件的面板,我想在点击边框时移动窗口。有可能吗?

是的,很有可能。你需要一个鼠标听者来监听鼠标事件。你开始移动鼠标向下,然后停止移动鼠标。然后,您只需按照鼠标在该阶段的平移量平移窗口位置(计算旧鼠标位置和新鼠标位置之间的增量,并将其添加到帧位置)。使用鼠标侦听器应该可以相当轻松地完成此操作

我有一个40像素大小的无光边框主面板,还有几个内部有控件的面板,我想在点击边框时移动窗口


我认为@camickr适合您

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

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);
        }
    });
    }
}
我使用这段代码已经有一段时间了,可以为未装饰的窗口创建自定义标题栏。 注意:您可以通过扩展JComponent而不是JPanel来概括这个示例

int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
thisX+-thisX=0

int xMoved = e.getX()-initialClick.x;
我用的是什么

public class MouseLiestenerX implements MouseListener,MouseMotionListener{

private theFrame;

public MouseLiestenerX(Frame theFrame){
this.theFrame = theFrame;

}

private Point startClick;

public void mouseDragged(MouseEvent e) {
    int deltaX = e.getX()-startClick.x;
    int deltaY = e.getY()-startClick.y;

    Core.getSp().setLocation(theFrame.getLocation().x+deltaX, theFrame.getLocation().y+deltaY);
}

public void mousePressed(MouseEvent e) {
    startClick = e.getPoint();
}

public void mouseMoved(MouseEvent e){

}
@Override
public void mouseClicked(MouseEvent e) {


}
@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent e) {

}
}

在你的框架构造器中

MouseLiestenerX IMove = new MouseLiestenerX(this);      
addMouseListener(IMove);
addMouseMotionListener(IMove);

我从我的项目中得到了一个简单的解决方案。这是我未修饰的JDialog类

public class TimerDialog extends JDialog {
// some fields here
private Point mouseClickPoint; // Will reference to the last pressing (not clicking) position

private TimerDialog() {
    initComponents();
    addEventsForDragging();
}

private void addEventsForDragging() {
    // Here is the code does moving
    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            mouseClickPoint = e.getPoint(); // update the position
        }

    });
    addMouseMotionListener(new MouseAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            Point newPoint = event.getLocationOnScreen();
            newPoint.translate(-mouseClickPoint.x, -mouseClickPoint.y); // Moves the point by given values from its location
            setLocation(newPoint); // set the new location
        }
    });
}

private void initComponents() {
    setLayout(new FlowLayout());
    // adding components
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setAlwaysOnTop(true);
    setUndecorated(true);
    setResizable(false);
    pack();
}
}

如果它是一个标签,那么就不需要在标题中包含Java。谢谢,这正是我想要的!我不知道如何,但这一个更好地工作在我的HiDPI屏幕上