Java - 如何简单地分配大量颜色?

时间:2014-02-25 23:22:35

标签: java

我只是想知道是否有办法让这段代码更简单。 感谢。

private void btnBlueActionPerformed(java.awt.event.ActionEvent evt) {                                        
    btnConvert.setBackground(new java.awt.Color(84, 209, 241)); //Changes the colors to blue
    btnReset.setBackground(new java.awt.Color(84, 209, 241));   //Changes the colors to blue
    btnClose.setBackground(new java.awt.Color(84, 209, 241));   //Changes the colors to blue
    btnInfo.setBackground(new java.awt.Color(84, 209, 241));    //Changes the colors to blue

    txtIncome.setBackground(new java.awt.Color(127, 228, 255)); //Changes the colors to blue
    txtPayable.setBackground(new java.awt.Color(127, 228, 255));//Changes the colors to blue
    txtStatus.setBackground(new java.awt.Color(127, 228, 255)); //Changes the colors to blue
    txtIncome.setForeground(new java.awt.Color(89, 89, 89));    //Changes the colors to blue
    txtPayable.setForeground(new java.awt.Color(89, 89, 89));   //Changes the colors to blue
    txtStatus.setForeground(new java.awt.Color(89, 89, 89));    //Changes the colors to blue
}                                       

3 个答案:

答案 0 :(得分:2)

不是一遍又一遍地重新创建相同的颜色,而是只需创建一次,然后根据需要分配它们:

Color btnColor = new Color(84, 209, 241);
Color txtColor1 = new Color(127, 228, 255);
Color textcolor2 = new Color(89, 89, 89);

然后:

private void btnBlueActionPerformed(java.awt.event.ActionEvent evt) {
    btnConvert.setBackground(btnColor);                                    
    btnReset.setBackground(btnColor);                                      
    btnClose.setBackground(btnColor);                                      
    btnInfo.setBackground(btnColor);                                       
    txtIncome.setBackground(txtColor1);                                    
    txtPayable.setBackground(txtColor1);                                   
    txtStatus.setBackground(txtColor1);                                    
    txtIncome.setForeground(textcolor2);                                       
    txtPayable.setForeground(textcolor2);                                      
    txtStatus.setForeground(textcolor2);                                        
}  

答案 1 :(得分:0)

看起来您的对象实际上应该是分组在一起的集合的一部分。

// assuming "mainMenuButtons" (or whatever) contains your first four objects
// note I don't know what color (84, 209, 241) actually is
for(Button btn : mainMenuButtons) {
    btn.setBackground(new java.awt.Color(84, 209, 241));
}

其次这些颜色是不可变的。没有必要创建这么多的对象

Color reddish = new java.awt.Color(84, 209, 241);
// then just use reddish everywhere instead of creating a new color each time.

答案 2 :(得分:0)

要简化以后可能要进行的代码和更改,请执行以下操作:

  1. 为应共享相同前景或背景的组件创建容器
  2. 为颜色创建容器(按州索引)
  3. 创建一个小型ComponentUtil类,允许您更改容器的所有成员的颜色
  4. 当一个动作改变了组件的状态时,你会为组件选择正确的颜色并调用ComponentUtil.setBackground(组件,颜色);或ComponentUtil.setForeground(components,color);

    import java.awt.Color;
    import java.awt.Component;
    public class ComponentUtil {
    
    public static void setBackground(Component[] components, Color color) {
        for (int i=0; i < components.length; i++) {
            components[i].setBackground(color);
        }
    }
    
    public static void setForeground(Component[] components, Color color) {
        for (int i=0; i < components.length; i++) {
            components[i].setForeground(color);
        }
    }
    }