有没有一种方法可以使Java应用程序的框架停留在我使用.setLocation设置的位置?我想禁用用户将其移动到屏幕上的能力。
以下是如何防止JFrame被移动。在生产应用程序中使用此方法不是个好主意,因为它会使用户感到烦恼,并在用户尝试最小化/最大化窗口时产生其他错误。
public static void main(String args[]) throws Exception {
class JFrameTypeLocking extends JFrame {
Point locked=null;
public JFrameTypeLocking(String string) {
super(string);
super.addComponentListener(new ComponentAdapter(){
public void componentMoved(ComponentEvent e) {
if (locked!=null) JFrameTypeLocking.this.setLocation(locked);
}});
}
public void lockLocation() {
locked=super.getLocation();
}
}
JFrameTypeLocking f = new JFrameTypeLocking("");
f.setSize(300,300);
f.setLocation(300,300);
f.setVisible(true);
f.lockLocation();
}
你无法禁止移动JFrame窗体,因为它是系统GUI的一部分。最好的方法是向JFrame添加ComponentListener,在移动时将其放回原处。
我认为这样做会产生很有趣的视觉效果。
这有点像是一个技巧,但你可以将它变成全屏。无法移动全屏应用程序。