'Can someone give me a really simple example for changing the color of text?

I have searched the Internet for a very simple example, but all of them were too complex and I couldn't understand them. Here is my code, but my linter said there was an error and I have no idea why. (I would like to request a REALLY simple example)

import javax.swing.*;
import javax.swing.text.*;
import java.awt.Color;
public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub        
    setPenColor(Color.RED);
    System.out.println("Red text!");
  }
}


Solution 1:[1]

Go here and download jansi.jar and then extract these folders and all sub directories: META-INF and org.fusesource.* everything from these. Put those in the same directories as your project.

    import org.fusesource.jansi.AnsiConsole;
    import static org.fusesource.jansi.Ansi.*;
    import static org.fusesource.jansi.Ansi.Color.*;

    public class test{
        public static void main(String[] args){
            try{
                AnsiConsole.systemInstall();
                System.out.println(ansi().fg(GREEN).a("Hello").reset() +
 " " + ansi().fg(RED).a("World").reset());      
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }

This is for windows console, so the text should appear to have color in the console^^.

enter image description here

Solution 2:[2]

What you are asking (changing java's console text color) isn't a feature of Java itself, but rather a feature of the IDE you are running your programs in (or command prompt, if your programs run in a command window). And changing these will change it for all text in all programs that use that console, and not just part of a program.

While the IDEs differ in how they change console color, they are usually under the IDE settings, and not set through code.

If you wanted to change the command window's text color, you can do so temporarily through the Properties window (right click on the title), and permanently on future openings through the defaults window (same steps as Properties, but a different menu item).

Solution 3:[3]

try this:

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class Test {

    public static void main(String[] args)  {

        JFrame frame = new JFrame();
        frame.setLayout(null);

        frame.setSize(400, 600); //width, height
        frame.setTitle("MyFrame something");

        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel myLabel = new JLabel();
        myLabel.setText("Blue Color");
        myLabel.setSize(100, 30); //width, height
        myLabel.setLocation(frame.getWidth()/2 - myLabel.getWidth()/2, frame.getHeight()/2 - myLabel.getHeight()/2);
        //      myLabel.setForeground(new Color(40, 60, 255, 255)); // red, green, blue, alpha/transparency from 0-255
        myLabel.setForeground(Color.blue); 

        JLabel myLabel_red = new JLabel();
        myLabel_red.setText("Red Color");
        myLabel_red.setSize(100, 30); //width, height
        myLabel_red.setLocation(frame.getWidth()/2 - myLabel_red.getWidth()/2, frame.getHeight()/2 - myLabel_red.getHeight()/2 + 50);
        myLabel_red.setForeground(Color.red); 

        JLabel myLabel_gray = new JLabel();
        myLabel_gray.setText("gray Color");
        myLabel_gray.setSize(100, 30); //width, height
        myLabel_gray.setLocation(frame.getWidth()/2 - myLabel_gray.getWidth()/2, frame.getHeight()/2 - myLabel_gray.getHeight()/2 + 100);
        myLabel_gray.setForeground(Color.gray); 


        frame.add(myLabel);
        frame.add(myLabel_red);
        frame.add(myLabel_gray);

        frame.setVisible(true);

        frame.repaint();


    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Azulflame
Solution 3 Brad Larson