对于标题提前抱歉,我不太清楚这是什么。
我正在尝试设置我的JFrame,以便当它越过屏幕宽度(或高度)时,它会停止移动。这是我到目前为止的代码......
public void moverMouseDragged(java.awt.event.MouseEvent evt) {
int x = evt.getXOnScreen()-xMouse;
int y = evt.getYOnScreen()-yMouse;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width2 = (int) screenSize.getWidth();
int height2 = (int) screenSize.getHeight();
if(this.getX()<0){x=0;}
if(this.getY()<0){y=0;}
if((this.getX()+width)>width2){x=width2;}
if((this.getY()+height)>height2){y=height2;}
this.setLocation(x, y);
System.out.println(this.getY()+"\n"+this.getX());
}
public void moverMousePressed(java.awt.event.MouseEvent evt) {
xMouse = evt.getX();
yMouse = evt.getY();
}
这仅适用于屏幕的顶部和左侧。它充满了毛刺,老实说我不知道从哪里去。
答案 0 :(得分:1)
我修好了它:
public void moverMouseDragged(java.awt.event.MouseEvent evt) {
int x = evt.getXOnScreen()-xMouse;
int y = evt.getYOnScreen()-yMouse;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int xScreen = (int) screenSize.getWidth();
int yScreen = (int) screenSize.getHeight();
//System.out.println(width+" "+height);
if(x<0){x=0;}
if(y<0){y=0;}
if(y>(yScreen-height)){y=yScreen-height;}
if(x>(xScreen-width)){x=xScreen-width;}
this.setLocation(x, y);
//System.out.println(this.getY()+"\n"+this.getX());
}
public void moverMousePressed(java.awt.event.MouseEvent evt) {
xMouse = evt.getX();
yMouse = evt.getY();
}
完全应该做什么:阻止JFrame越过屏幕的四边。