Pergunta

I'm getting magic numbers for jpanel background. Is there a way to get rid of this since it's bad practice:

panel.setBackground(new color (255,255,0));

How can I have custom colors too?

Foi útil?

Solução

There are some common colors. such as red,blue, etc.

panel.setBackground(Color.YELLOW);

but if you want to set your own colors you must create a object with your colors,

public static final Color myColor = new Color(55,265,44);

then set it,

panel.setBackground(myColor);

Outras dicas

Just define your own palette somewhere, eg:

class Colors {
  public static final Color BACKGROUND_COLOR = new Color(55,265,44);
  public static final Color PALE_RED = new Color(...);
}

panel.setBackground(Colors.BACKGROUND_COLOR);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top