-4

I am trying to create a simple Java program. Now it just takes and displays the user input. Since I am quite new to this language I wonder how can I style the program to make it look more appealing to user? I tried to google it but I start to think that it is impossible to style java programs... I am using eclipse workspace and created new .java file

Here is my program code:

import java.util.Scanner;
    public class Hello {

    public static void main(String[] args) {          
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int number = input.nextInt();
        System.out.println("You entered " + number);
    }
}

So, how to style my program? Maybe I could style (for example, add size, font to text, create a button and change color of it) using css somehow? Or any other technology? How can I join java ant style?

vytaute
  • 1,260
  • 4
  • 16
  • 36
  • This is probably too broad of a question, there are many different libraries that can be used to create a GUI in Java. See this [post](https://stackoverflow.com/questions/7358775/java-gui-frameworks-what-to-choose-swing-swt-awt-swingx-jgoodies-javafx) – Nexevis Sep 25 '19 at 20:05
  • 1
    You are going to be very disappointed by the options available through `System.out.print` for styling. Plain ASCII text. No size. No font. No buttons. No colors. No css. If you need any of those things, you'll need something other than `System.out.print`. – Elliott Frisch Sep 25 '19 at 20:06
  • 1
    You have a console application here, which is strictly text based and looks are dependent on the console you're running it in. Perhaps you need to find yourself a tutorial on creating a Graphical User Inserface (GUI) in Java. – jmoerdyk Sep 25 '19 at 20:08
  • 1
    Thank you, will check what you suggested me. But no need to down vote my question. I am just looking for a help because I could not find it in the internet even though I was looking for like 5 hours today. So it might be as well useful for others. Thanks – vytaute Sep 25 '19 at 20:12
  • It's being down voted because it's off topic for this site. It's far too broad, and asking for tool/framework recommendations and/or a tutorial, all of which are off-topic, see the [help/on-topic] for details. – jmoerdyk Sep 25 '19 at 20:19
  • google `java gui options` – Pavel Sep 25 '19 at 22:23

1 Answers1

-1
If your terminal supports it, you can use ANSI escape codes to use color in your output. It generally works for Unix shell prompts; however, it doesn't work for Windows Command Prompt (Although, it does work for Cygwin). For example, you could define constants like these for the colors:

public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
Then, you could reference those as necessary.

For example, using the above constants, you could make the following red text output on supported terminals:

System.out.println(ANSI_RED + "This text is red!" + ANSI_RESET);

You might want to check out the Jansi library. It provides an API and has support for Windows using JNI. I haven't tried it yet; however, it looks promising.

Also, if you wish to change the background color of the text to a different color, you could try the following as well:

public static final String ANSI_BLACK_BACKGROUND = "\u001B[40m";
public static final String ANSI_RED_BACKGROUND = "\u001B[41m";
public static final String ANSI_GREEN_BACKGROUND = "\u001B[42m";
public static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m";
public static final String ANSI_BLUE_BACKGROUND = "\u001B[44m";
public static final String ANSI_PURPLE_BACKGROUND = "\u001B[45m";
public static final String ANSI_CYAN_BACKGROUND = "\u001B[46m";
public static final String ANSI_WHITE_BACKGROUND = "\u001B[47m";

For instance -

System.out.println(ANSI_GREEN_BACKGROUND + "This text has a green background but default text!" + ANSI_RESET);
System.out.println(ANSI_RED + "This text has red text but a default background!" + ANSI_RESET);
System.out.println(ANSI_GREEN_BACKGROUND + ANSI_RED + "This text has a green background and red text!" + ANSI_RESET);
catBoi
  • 19
  • 5