gpt4 book ai didi

java - 如何设置 JRadioButtons 的属性并将其应用于所有属性?

转载 作者:行者123 更新时间:2023-12-01 18:16:43 25 4
gpt4 key购买 nike

Hi, I have over 10 JRadio Buttons and there are some properties they all have in common, so instead of writing these properties one by one to each radio button, is there are way to set them once for all?

The properties I want to set for all radio buttons are:

radiButtonName.setOpaque(false);
radiButtonName.setContentAreaFilled(false);
radiButtonName.setBorderPainted(false);
radiButtonName.setBorder(null);

I tried using UIManager but it's acting strange and it doesn't support all of the properties i require.

I would like to avoid creating additional class and extend radio button. Because I would also like to apply this technique to other components to reduce the code written, this will make the code much shorter. Thank you in advance :)

最佳答案

如果您必须多次执行相同的操作,请尝试编写一个可以为您解决问题的方法:)

所以这里的问题是设置10个JRadioButton?您可以创建一个返回 JRadio 按钮列表(或另一个集合/ map /...)的方法,如下所示:

private List<JRadioButton> setUpButtons() {
//create list
for(int i = 0; i < NB_BUTTONS; i++) {
//set the properties wanted
}
return myList;
}

请注意,NB_BUTTONS 是一个类似这样的变量:

private final int NB_BUTTONS = 10; 

修改一次值比在所有代码中修改它更容易。使用相同的逻辑,您可以编写一种“准备”一个 JRadioButton 的方法。这取决于您想如何做您想做的事情。

关于java - 如何设置 JRadioButtons 的属性并将其应用于所有属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29079876/

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