Question

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?

Était-ce utile?

La solution

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);

Autres conseils

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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top