0

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!");
  }
}
tomtomssi
  • 1,017
  • 5
  • 20
  • 33
C. Yang
  • 41
  • 1
  • 7
  • Possible duplicate of [Set the text color in a Java textbox](http://stackoverflow.com/questions/900360/set-the-text-color-in-a-java-textbox) – Tersosauros Mar 06 '16 at 05:57
  • 1
    I don't understand a word of that question. Sorry :( – C. Yang Mar 06 '16 at 06:00
  • @Tersosauros No, because that question is asking about the JTextbox while this is asking about the java console. – Paul Mar 06 '16 at 06:05
  • 1
    @Paul Ahh, I see. Hard to know when the code imports swing (for no reason?) and the question doesn't use the word "console" anywhere! – Tersosauros Mar 06 '16 at 06:08
  • Then study and study until you understand every word of that question. Changing the color of text in a JTextArea is **extremely** complicated. – Gilbert Le Blanc Mar 06 '16 at 07:49
  • Possible duplicate of [How to print color in console using System.out.println?](http://stackoverflow.com/questions/5762491/how-to-print-color-in-console-using-system-out-println) – fabian Mar 06 '16 at 20:25

3 Answers3

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

chewpoclypse
  • 500
  • 5
  • 20
0

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

Azulflame
  • 1,534
  • 2
  • 15
  • 30
0

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


    }
}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
GabeTheApe
  • 348
  • 2
  • 13