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

标签 java swing user-interface look-and-feel 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 :)

最佳答案

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

所以这里的问题是设置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/

相关文章:

java - 寻找数组中最好的人

java - 无法使 JDialog 模式化

java - 未设置 JPanel 首选大小

java - 警报类型无关闭 javafx

java - 我如何通过推送 primeFaces 来刷新 dataTable

Java spring忽略请求中的字段

java - 如何在 JTextPane 中嵌入可调整大小的 JPanel?

java - 如何在 JFrame 中添加其他类的输出?

java - GUI编程和上传图像?

java - "Always on Top"带有 Java 的 Windows