Java,JFrame我不想让用户通过右上角的叉退出程序,所以我想不显示右上角的叉,或者点击右上角叉时没有反应。
只要设置JFrame没有标题,取消标题,代码如下:
//隐藏标题栏
setUndecorated(true);
可以实现,只要监听窗体关闭事件。不做处理即可。
给你示范一下:
效果图:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MyFrame extends JFrame {
MyFrame() {
}
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.out.println("点击了关闭x");
} else {
super.processWindowEvent(e);
}
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setSize(new Dimension(500, 500));
frame.setVisible(true);
}
}
如有帮助,望点击我回答右上角【采纳】按钮支持一下。