In Java AWT, using Color object, the background color of frame or any component or any graphical figure can be changed. In this tutorial set Background Color Java, the background color of frame, button and graphics is changed. Know how to create different colors with Java AWT Color.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.awt.*; public class SetBackgroundColor extends Frame { public SetBackgroundColor() { setBackground(Color.cyan); // sets predefined color cyan to background of frame setLayout(new FlowLayout()); Color clr1 = new Color(187, 157, 177); Button btn = new Button("CLICK HERE"); btn.setBackground(clr1); // sets background color to Button btn add(btn); setSize(300, 200); setVisible(true); } public void paint(Graphics g) { g.setColor(Color.red); g.fillRect(80, 100, 150, 75); } public static void main(String args[]) { new SetBackgroundColor(); } } |
Output Screenshot…