如何设置 JRadioButtons 的属性并应用于它们?
How to set the properties for JRadioButtons and apply to them all?
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 :)
如果您必须做 N 次相同的事情,请尝试编写一种方法来解决问题:)
所以这里的问题是设置10个JRadioButton?您可以创建一个返回 JRadio 按钮列表(或另一个 collection/maps/...)的方法,如下所示:
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;
修改一次值比在所有代码中修改它更容易。
使用相同的逻辑,您可以编写方法 'preparing' 一个 JRadioButton。这取决于你想如何做你想做的事情。
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 :)
如果您必须做 N 次相同的事情,请尝试编写一种方法来解决问题:)
所以这里的问题是设置10个JRadioButton?您可以创建一个返回 JRadio 按钮列表(或另一个 collection/maps/...)的方法,如下所示:
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;
修改一次值比在所有代码中修改它更容易。 使用相同的逻辑,您可以编写方法 'preparing' 一个 JRadioButton。这取决于你想如何做你想做的事情。