自定义 Java 窗口标题栏菜单

发布于 2024-11-16 10:31:04 字数 447 浏览 5 评论 0原文

我试图允许用户更改 Java 中窗口的标题,而无需向窗口本身添加组件。我实际上正在使用 JInternalFrame 尝试此操作,但认为解决方案对于 JFrame 应该类似。我只想在右键单击窗口标题栏时弹出的上下文菜单中添加一个附加菜单项。例如,下面的设置标题

在此处输入图像描述

此示例适用于 Windows XP,但也许有一种方法可以独立地获取窗口上下文菜单操作系统,可能类似于 SystemTray.getSystemTray()(但对于应用程序内的各个窗口)。由此,我将能够提供自己的 ActionListener 来弹出一个对话框,供用户输入新标题。

这是一个比我想象的要大得多的任务吗?有人有他们以前使用过的解决方案吗?

I'm trying to allow the user to change the title of a window in Java without adding components to the window itself. I'm actually trying this with a JInternalFrame, but figure the solution should be similar for a JFrame. I simply want to add an additional menu item in the context menu that pops up when right clicking on a window title bar. For example, the Set title below:

enter image description here

This example is on Windows XP, but perhaps there's a way to get the window context menu OS independently perhaps similar to the SystemTray.getSystemTray() (but for individual windows within an application). From this I would be able to provide my own ActionListener to popup a dialog for the user to enter a new title.

Is this a much bigger task than I'm guessing it is? Does anyone have solutions they've used before?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽容 2024-11-23 10:31:04

简短的回答:我认为这并不容易。我不确定这是否可能。

首先,JFrame 和 JInternalFrame 实际上是有很大不同的。 JFrame 是一个顶级组件,其标题栏等通常由操作系统提供。 JInternalFrame的全部内容(包括标题栏)都是由Swing LAF提供的。

对于 JInternalFrame,上下文菜单由 LAF 提供,而不是 JInternalFrame 本身。您必须对 UIComponent 执行某些操作才能更改上下文菜单。我认为您可能必须提供一个自定义 UI 组件才能做到这一点,这很丑陋,通常会跨不同的 LAF 或工作,但最多看起来很糟糕。请参阅 BasicInternalFrameTitlePane,createSystemMenu 方法。

Short answer: I don't think this is easy. I'm not 100% sure if it is possible.

First, JFrame and JInternalFrame are actually quite different. JFrame is a top level component whose title bar and such are typically provided by the OS. JInternalFrame's entire content (including title bar) is provided by the Swing LAF.

For a JInternalFrame, the context menu is provided by the LAF, not JInternalFrame itself. You would have to do something with the UIComponent in order to change the context menu. I think you would likely have to provide a custom UI component in order to do this, which is ugly and typically breaks across different LAFs or works but looks terrible at best. See BasicInternalFrameTitlePane, the createSystemMenu method.

阿楠 2024-11-23 10:31:04

我认为如果不深入研究 Swing 的内部 UI 系统这是不可能的,我什至不会考虑这样做。为什么不使用JInternalFrame内置的JMenuBar呢?

myInternalFrame.setJMenuBar(myMenuBar);

I don't think this is possible without digging way too deep into Swing's internal UI system and I wouldn't even consider doing this. Why don't you use the inbuilt JMenuBar of JInternalFrame?

myInternalFrame.setJMenuBar(myMenuBar);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文