我有一个未修饰的 JFram
setUndecorated(true);
,我在顶部有一个 JPanel headerPanel
,我用它来单击并拖动以移动 JFrame。但是,当我移动它时,框架不会超出屏幕边界。如果我向右单击并拖动,当右侧到达屏幕的右边界时,框架停止移动。如果我setUndecorated(false);
框架可以被拖到屏幕边界之外。
这是我在单击并拖动 JPanel 时如何移动 JFrame 的代码
headerPanel
:
// Add mouse listener for headerPanel mb
headerPanel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent me) {
if (getExtendedState() == UIFrame.MAXIMIZED_BOTH) {
//don't allow moving the frame when is it maximized
return;
}
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
// Get x,y and store them
winLocationX = me.getX();
winLocationY = me.getY();
}
});
headerPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent me) {
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
});
// Add MouseMotionListener for detecting drag
headerPanel.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent me) {
if (getExtendedState() == UIFrame.MAXIMIZED_BOTH) {
//don't allow moving the frame when is it maximized
return;
}
// get the current location x-co-ordinate and then get
// the current drag x co-ordinate, add them and subtract most recent
// mouse pressed x co-ordinate
// do same for y co-ordinate
setLocation(getLocation().x + me.getX() - winLocationX, getLocation().y + me.getY() - winLocationY);
}
});
我做错了什么不允许我将
JFrame
移到屏幕边界之外?