在 JMenu 组件中设置标题文本的对齐方式 [英] Setting alignment of title text in a JMenu component
问题描述
简单地说,JMenu.setHorizontalAligment(SwingConstants.CENTER) 什么都不做,文本仍然左对齐(使用 Windows LAF).
Simply put, JMenu.setHorizontalAligment(SwingConstants.CENTER) does nothing and the text is still left-aligned (using the Windows LAF).
是否有任何方法可以对齐菜单标题中的文本(不是 JMenuItem - JMenu 本身)?
Is there any method of aligning the text in a menu header (not the JMenuItem - the JMenus themselves)?
我使用的是 JDK1.7.
I'm using JDK1.7.
我的 JMenuBar 及其 JMenus 目前的样子:
What my JMenuBar with its JMenus looks like currently:
推荐答案
我使用 CSS
来定位菜单中的文本并设置其样式.style="text-align:center;"
是 CSS
用于将文本定位在底部示例图像中的中心.就我而言,我将 JMenu
标题封装在 段落标签中;
I've used CSS
to position and style the text in the menus. style="text-align:center;"
is the CSS
used to position the text in the center in the example image at the bottom. In my case, I encapsulated the JMenu
title in <p>
paragraph tags;
clientFilter = new JMenu("<html><p style='text-align:center;'>Client</p></html>");
siteFilter = new JMenu("<html><p style='text-align:center;'>Site</p></html>");
employeeFilter = new JMenu("<html><p style='text-align:center;'>Employee</p></html>");
jobtypeFilter = new JMenu("<html><p style='text-align:center;'>Job Type</p></html>");
看起来像这样;
值得注意的是,在上图中,我还在 style 属性中使用了 width:90px;color:blue;
,但为了简单起见,我从上面的代码示例中删除了这两种样式.希望这会有所帮助.
It's worth noting that in the image above I have also used width:90px;color:blue;
in the style attribute, but I removed those two styles from the code example above for simplicity. Hope this helps.
这个答案或多或少是我原始答案的副本这里一>.
This answer is more or less a copy of my original answer here.
这篇关于在 JMenu 组件中设置标题文本的对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!