Java实现截图功能,进行保存图片时,保存窗口在拖拽的时候桌面显示为异常如何设置JFileChooer窗口为固定窗口,不可拖拽
1条回答
- 明初啥都能学会 2024-04-03 14:56关注
要实现在 Java 中的截图功能,并确保保存窗口在拖拽时不会显示异常,您可以使用 Java Swing 库中的 JFileChooser 类来创建保存文件对话框,并通过设置对话框的 modalityType 属性来禁用拖拽。
以下是一个简单的示例代码,展示了如何使用 JFileChooser 创建一个保存文件对话框,并将其设置为不可拖拽:
import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; public class ScreenshotApp extends JFrame { public ScreenshotApp() { setTitle("Screenshot Application"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 200); setLocationRelativeTo(null); // Center the frame JButton saveButton = new JButton("Save Screenshot"); saveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Save Screenshot"); // 设置文件过滤器 FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG Images", "png"); fileChooser.setFileFilter(filter); // 设置对话框不可拖拽 setModalityType(Dialog.ModalityType.APPLICATION_MODAL); int userSelection = fileChooser.showSaveDialog(ScreenshotApp.this); if (userSelection == JFileChooser.APPROVE_OPTION) { File fileToSave = fileChooser.getSelectedFile(); // 在此处执行保存截图的逻辑 // 这里只是示例,需要根据您的实际需求来实现保存逻辑 System.out.println("Save screenshot to: " + fileToSave.getAbsolutePath()); } } }); getContentPane().setLayout(new FlowLayout()); getContentPane().add(saveButton);
解决 无用评论 打赏 举报
悬赏问题
- ¥15 程序不包含适用于入口点的静态Main方法
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记