在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);
}
}