简体   繁体   English

如何制作与文本大小完全相同的按钮?

[英]How can I make a button exactly the same size of its text?

Is it possible to make a JButton take exactly the size of its text? 是否有可能使JButton完全符合其文本的大小? Since by default, a JButton will have a small amount of padding (both horizontal and vertical) around its text. 因为默认情况下, JButton在其文本周围会有少量填充(水平和垂直)。 I would like to remove that padding. 我想删除那个填充。

JButton has a border by default, you can remove it: JButton默认有边框,你可以删除它:

button.setBorder(null);

If you want to keep the border, reduce the margin (see Eugenes answer ) 如果你想保持边界,减少边际(见Eugenes答案

Clear out the margins in the button with setMargin . 使用setMargin清除按钮中的边距。

Removing the border has the side-effect of removing the border. 删除边框具有删除边框的副作用。 If you only want to clear out the margins, use the setMargin method to set the margins all to zero. 如果您只想清除边距,请使用setMargin方法将边距全部设置为零。

button.setMargin(new Insets(0,0,0,0));

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

相关问题 如何筛选完全垂直的尺寸和完全水平的尺寸? - How can I screen a full vertical size with exactly horizontal size? 如何制作与面板相同尺寸的图像? - How can I make an image the same size as the panel? 更改大小后如何使纯文本中的文本可见? - How to make text visible in Plain Text after changing its size? 当我在SWT中更改按钮的文本时,整个按钮的大小都会更改。 如何禁用此功能? - When I change the text for the button in SWT, the whole button size changes. How can I disable this? 如何创建一个包含许多相同对象的构造函数,但如果一个对象为空则忽略它? - How can I make a constructor that contains a number of the same objects but if one its empty to ignore it? javafx:如何使标签根据字符串属性自动更新其文本颜色? - javafx: How can I make a label automatically updates its text color depending on a String Property? 如何设置按钮的大小? (摇摆) - How can I set size of button? (Swing) 当“编辑文本”框中没有文本时,如何使按钮不可单击? - How can I make a button unclickable when there is no text in the Edit Text box? 如何设置按钮的大小? - How can I set size of a button? 如何更改按钮的大小和 position - How can i change size and position of button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM