简体   繁体   English

JButton中的图标位置

[英]Icon position in JButton

Is there any posibility of changing Icon image posintion in JButton? 在JButton中是否有改变Icon图像位置的可能性?

This is how it looks now: 这就是它现在的样子:

在此输入图像描述

I want to move the icon more to the left. 我想将图标更多地移到左边。

I have tried to change the text alignment but it doesn't work as I want: 我试图更改文本对齐但它不能按我的意愿工作:

myButton.setHorizontalTextPosition(SwingConstants.RIGHT);

Have a look at JButton.setHorizontalAlignment(int align) 看看JButton.setHorizo​​ntalAlignment(int align)

Sets the horizontal alignment of the icon and text. 设置图标和文本的水平对齐方式。 AbstractButton's default is SwingConstants.CENTER, but subclasses such as JCheckBox may use a different default. AbstractButton的默认值是SwingConstants.CENTER,但是诸如JCheckBox之类的子类可能使用不同的默认值。

you can set: 你可以设置:

  • SwingConstants.RIGHT SwingConstants.RIGHT
  • SwingConstants.LEFT SwingConstants.LEFT
  • SwingConstants.CENTER SwingConstants.CENTER
  • SwingConstants.LEADING SwingConstants.LEADING
  • SwingConstants.TRAILING SwingConstants.TRAILING

You can change the gap of the space between the icon and text with setIconTextGap(int) . 您可以使用setIconTextGap(int)更改图标和文本之间的setIconTextGap(int) In addition, aligning the contents of the JButton towards the left may help with setHorizontalAlignment(SwingConstants.LEFT) . 此外,将JButton的内容向左对齐可能有助于setHorizontalAlignment(SwingConstants.LEFT)

SOLUTION

I've added an empty border(you can add any type of border) to the myButton and now it look like that: 我已经添加了一个空边框(你可以添加任何类型的边框)到myButton,现在看起来像这样:

在此输入图像描述

Code: 码:

myButton.setBorder(BorderFactory.createEmptyBorder(4, 4, 2, 20));

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

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