在Java中,可以通過設置窗體的setUndecorated方法來設置窗體不可移動。具體步驟如下:
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
// Do nothing
}
});
完整代碼示例:
import javax.swing.*;
import java.awt.event.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
// Do nothing
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
}