Java 设置窗口的 "Document"

标签 java swing document title

在某些Mac应用程序中,当打开文档时,窗口的标题将成为文档名称,并且窗口将具有图标。右键单击标题会显示该文件的路径。我想知道是否有办法在 Java 中做到这一点。

enter image description here

最佳答案

您可以通过设置 JFrame 的 rootPane 的 "Window.documentFile" 客户端属性来实现此目的。来自 https://developer.apple.com/library/mac/technotes/tn2007/tn2196.html#WINDOW_DOCUMENTFILE :

Window.documentFile

Type: java.io.File

Applies to: JRootPane

This property adds a document proxy icon to the title bar of the window. This icon is the effective representation of the document that can be dragged and dropped into the Finder or the Dock. Command-clicking on the title will present the full path to the document in a popup. The proxy icon in the title bar will have the icon of document as presented by LaunchServices in the Finder or Open and Save dialog boxes. This property can be changed at anytime throughout the lifetime of the window.

示例代码:

public void open(File file) {
    JFrame frame = new JFrame("My App");
    frame.getRootPane().putClientProperty("Window.documentFile", file);
    frame.getRootPane().putClientProperty("Window.documentModified", false);

    // Load file
}

关于Java 设置窗口的 "Document",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20205451/

相关文章:

Java jdom不打印所有元素,打印空字符串

java - 在 Kotlin 中使用 Room 的 @ForeignKey 作为 @Entity 参数

java - 如何使用 maven 下载 JavaMail (mail.jar) 和 JAF (activation.jar)

java - 将 MySQL 查询结果设置到 JComboBox 中

javascript - [Vue警告] : Error in beforeCreate hook: "ReferenceError: document is not defined"

git - 如何通过 git 共享 odt/doc 文档

java - Spring JDBC中如何组合多个参数源?

java - 为什么我在这里得到 JsonSyntaxException?

java - DefaultTableModel 上的类转换异常

java - 为什么 JOptionPane.showInputDialog 不需要父组件参数,但 JOptionPane.showConfirmDialog 需要?