JButton,其边框和按钮本身之间具有填充 [英] JButton with padding between its border and the button itself
问题描述
我有一个JButton,它的背景为绿色,边框为LineBorder.我想在按钮和边框之间插入一个空格,这是一种填充. 我已经尝试过setMargin(new Insets(x,y,t,z)),但似乎无法正常工作. 这是我的代码.
I've a JButton that has its background green-colored and its border as a LineBorder. I would like to insert a space between the button and the border, a kind of padding. I've tried with setMargin(new Insets(x,y,t,z)) but it seems not working. This is my piece of code.
JButton JBtn=new JButton("sdfd");
JBtn.setBorder(BorderFactory.createLineBorder(Color.CYAN,5));
JBtn.setBackground(Color.GREEN);
JBtn.setMargin(new Insets(5,5,10,10));
有什么建议吗?
推荐答案
边框是按钮的一部分,单击边框将单击按钮.您可以将背景设置为绿色,然后在背景上绘制边框:
The borders are part of the button and clicking on them will click the button. You can set the background as green, then paint borders over the background:
jBtn.setBackground(Color.GREEN);
jBtn.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.CYAN, 5),
BorderFactory.createLineBorder(Color.BLACK, 20)));
我已经尝试过使用setMargin(new Insets(x,y,t,z)),但似乎无法正常工作.
I've tried with setMargin(new Insets(x,y,t,z)) but it seems not working.
由于您阅读了setMargin
的文档,因此您会看到
Because if you read the documentation for setMargin
you'll see that
[...]如果在按钮上设置了非默认边框,则是Border对象负责创建适当的边距空间(否则此属性将被有效地忽略).
[...] if a non-default border is set on the button, it is that Border object's responsibility to create the appropriate margin space (else this property will effectively be ignored).
还要为类保留大写名称,将JBtn
重命名为jBtn
.
Also, reserve uppercase names for classes, rename JBtn
to jBtn
.
这篇关于JButton,其边框和按钮本身之间具有填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!