gpt4 book ai didi

java - 在 JMenu 组件中设置标题文本的对齐方式

转载 作者:搜寻专家 更新时间:2023-11-01 03:10:28 24 4
gpt4 key购买 nike

简单地说,JMenu.setHorizo​​ntalAligment(SwingConstants.CENTER) 什么都不做,文本仍然左对齐(使用 Windows LAF)。

是否有任何方法可以对齐菜单标题中的文本(不是 JMenuItem - JMenus 本身)?

我正在使用 JDK1.7。

我的 JMenuBar 及其 JMenus 目前的样子:

Current JMenuBar with JMenus (and a single JMenuItem

最佳答案

我用过 CSS在菜单中定位和设置文本样式。 style="text-align:center;"CSS用于将文本定位在底部示例图像的中心。在我的例子中,我封装了 JMenu标题 <p>段落标签;

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>");

看起来像这样;

enter image description here

值得注意的是,在上图中我还使用了 width:90px;color:blue;在样式属性中,但为了简单起见,我从上面的代码示例中删除了这两种样式。希望这会有所帮助。

此答案或多或少是我原始答案的副本 here .

关于java - 在 JMenu 组件中设置标题文本的对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11712667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com