Color Class in Java

Today, I will teach you the basics of having colors in Java. Colors may add life to your GUI Programs. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of 2. Import javax.swing and java.awt to have GUI components such as frames and buttons.
  1. import javax.swing.*;
  2. import java.awt.*;
3. Initialize a panel and a button for your global variable as we will call this to the constructor.
  1. JPanel pane = new JPanel();
  2. JButton b = new JButton("OK");
Now, create a constructor named colorClass that will have the color class. We will try to create a class for the RGB (Red, Green, Blue) color with a variable name color. And put it as the background color of our panel.
  1. Color color = new Color(122,54,3);
  2. pane.setBackground(color);
Where red, green, and blue are a integer between 0 and 255 to indicate the amount of red, green, and blue respectively. Now, we will create a background color of our button and we will have its color as static. Static means we will have a direct call of the color. Have and interpret this code below:
  1. b.setBackground(Color.cyan);
The cyan there is the color of the button. And there are 13 constant colors in java namely, black, blue, cyan, dark gray, gray, green, yellow, light gray, magenta, orange, pink, red, and white. Then put this code in the Main to have the GUI form.
  1. public static void main (String args[]) {
  2. JFrame frame= new colorClass();
  3. frame.setSize(300,300);
  4. frame.setVisible(true);
  5. frame.setResizable(false);
  6. }
Output: output Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: Visit and like my page on Facebook at:

Add new comment