-4

I am trying to change the colour of all 'A's in the string to green and all 'Z's in the string to red colour. I have a string as:

String input = "LENGTH OF THIS STRING IS GREATER THAN ZERO";

I want to output it on the screen (in SWING, see comments) with all letters 'A' colored to green and 'Z' colored to red.

Thomas Uhrig
  • 30,811
  • 12
  • 60
  • 80

1 Answers1

1

If you are using a Swing component (JLabel, JButton, J...) then you should use some HTML in your Swing component.

Here is an example from official Swing documentation:

button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>");

So you can do whatever you want with your text !

I guess this is not the case, but you can also want to 'draw' your text in Canvas, then you should read the documentation about Java2D API

Guillaume
  • 5,488
  • 11
  • 47
  • 83