gpt4 book ai didi

java - JButton 在其边框和按钮本身之间有填充

转载 作者:行者123 更新时间:2023-12-01 12:40:03 31 4
gpt4 key购买 nike

我有一个 JButton,其背景为绿色,边框为 LineBorder。我想在按钮和边框之间插入一个空格,一种填充。
我试过 setMargin(new Insets(x,y,t,z)) 但它似乎不起作用。
这是我的一段代码。

JButton JBtn=new JButton("sdfd");
JBtn.setBorder(BorderFactory.createLineBorder(Color.CYAN,5));
JBtn.setBackground(Color.GREEN);
JBtn.setMargin(new Insets(5,5,10,10));

有什么建议吗?

最佳答案

边框是按钮的一部分,单击它们将单击按钮。您可以将背景设置为绿色,然后在背景上绘制边框:

jBtn.setBackground(Color.GREEN);
jBtn.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.CYAN, 5),
BorderFactory.createLineBorder(Color.BLACK, 20)));

enter image description here

I've tried with setMargin(new Insets(x,y,t,z)) but it seems not working.



因为如果你阅读了 setMargin 的文档你会看到

[...] 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).



另外,为类保留大写名称,重命名 JBtnjBtn .

关于java - JButton 在其边框和按钮本身之间有填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30239318/

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