在 JMenu 中居中文本 [英] centering text in a JMenu

查看:29
本文介绍了在 JMenu 中居中文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我一直在网上寻找有关此问题的帮助,但我尝试的任何方法似乎都不起作用.我想让所有的菜单文本都集中在他们的菜单按钮上.当我使用 setHorizo​​ntalTextPosition(JMenu.CENTER) 时,没有任何变化.事实上,无论我使用什么常量,菜单文本都保持左对齐.

Alright, I have been cruising through the net looking for some help with this one, but nothing I try seems to work. I want to have all of the menu texts centered on their menu buttons. When I use setHorizontalTextPosition(JMenu.CENTER) there is no change. In fact, regardless of the constant I use, the menu text remains left justified.

有什么想法吗?

*更新 1

仍然没有运气.重新阅读 JMenu API 后,我意识到 setHorizo​​ntalTextPosition(int)setVerticalTextPosition(int) 只设置与图标相关的文本,而 setHorizo​​ntalAlignment(int))setVerticalAlignment(int) 设置 JMenu 中文本和图标的对齐方式.

Still no luck. After re-reading the JMenu API, I realized that setHorizontalTextPosition(int) and setVerticalTextPosition(int) only set the text in relation to an icon and setHorizontalAlignment(int) and setVerticalAlignment(int) set the alignment of both text and icon in the JMenu.

当与图标一起使用时,文本定位方法似乎像宣传的那样工作.

The text positioning methods seem to work as advertised when used with an icon.

垂直对齐方法似乎适用于文本或图标或两者.

The vertical alignment method seems to work on text or icon or both.

然而,水平对齐方法似乎根本不起作用.即使只是在文本或图标或两者上单独使用它,似乎也没有任何改变.我已将菜单大小增加到 150x50,无论组合如何,我都无法使文本或图标或水平居中.

However, the horizontal alignment method doesn't seem to work at all. Even just using it alone on text or icon or both, nothing seems to change. I have increased the menu size to 150x50 and, no matter the combination, I can't get the text or icon or either to center horizontally.

推荐答案

延续 MadProgrammer 使用 HTML 的主题,我使用 CSS 来定位和设置文本样式菜单.style="text-align:center;" 是用于将文本定位在中心的 CSS.在我的例子中,我将 JMenu 标题封装在

段落标签中的 元素中;

Going on MadProgrammer's theme of using HTML, I 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 my case, I encapsulated the JMenu title in a <p> paragraph tags inside an <html> element;

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.

这篇关于在 JMenu 中居中文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆