简体   繁体   English

将JButton文本移动到其图标上的像素

[英]Moving JButton text over its icon by pixels

I am creating a desktop application in Java with lots of customized UI. 我正在用Java创建一个带有大量自定义UI的桌面应用程序。 It also has a breadcrumb. 它也有面包屑。 I've extended JButton class to customize it for my Breadcrumb buttons. 我已经扩展了JButton类来为我的Breadcrumb按钮定制它。

This is the screenshot of expected breadcrumb. 这是预期的痕迹的截图。

http://puu.sh/4MtvZ.jpg http://puu.sh/4MtvZ.jpg

在此输入图像描述

The background of this breadcrumb is an ImageIcon. 这个面包屑的背景是ImageIcon。 But now, I am not able to perfectly align the JButton text over this background icon. 但是现在,我无法在这个背景图标上完美地对齐JButton文本。

This is the screenshot of actual breadcrumb. 这是实际面包屑的屏幕截图。

http://puu.sh/4MtDc.png http://puu.sh/4MtDc.png

在此输入图像描述

I've used following code to align the text. 我使用以下代码来对齐文本。

setHorizontalTextPosition(CENTER);
setVerticalTextPosition(CENTER);

So, is their a way that I can move this text in pixels to its right position? 那么,他们是否可以将此文本以像素移动到正确的位置?

And also, I want root breadcrumb button "Schemes" to overlap some part of its preceding breadcrumb button "2000 Avenues" as shown in expected breadcrumb screenshot! 而且,我希望root breadcrumb按钮“Schemes”重叠其前面的痕迹导航按钮“2000 Avenues”的某些部分,如预期的breadcrumb屏幕截图所示! How can I achieve that? 我怎样才能做到这一点?

Check out the Overlap Layout for one solution. 查看一个解决方案的重叠布局

So, is their a way that I can move this text in pixels to its right position? 那么,他们是否可以将此文本以像素移动到正确的位置?

I don't understand this question. 我不明白这个问题。 Why are you setting the alignment to center if you want it aligned to the left. 如果要将对齐方式设置为左侧,为什么要将对齐设置为居中。

Edit: 编辑:

I see. 我知道了。 You are just using an Icon for the button outline and are then painting the text on top of the Icon. 您只是使用按钮轮廓的图标,然后在图标顶部绘制文本。 I thought your were using a custom shaped button with text). 我以为你正在使用带文字的自定义形状按钮)。 You can just use the default vertical/horizontal text position settings. 您可以使用默认的垂直/水平文本位置设置。

To shift the text to the left you can use: 要将文本向左移动,您可以使用:

button.setIconTextGap(10 - button.getIcon().getIconWidth());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM