简体   繁体   English

Java 控制台扫描仪颜色

[英]Java Console Scanner Color

Let's say I have a console app in Java that reads input from the user and then prints it back out (very basic)假设我有一个 Java 控制台应用程序,它从用户那里读取输入,然后将其打印出来(非常基本)

import java.util.Scanner;导入 java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        while(true) {
            Scanner scanner = new Scanner(System.in);
            String message = scanner.nextLine();
            System.out.println(message);
        }
    }
}

Is it possible to make the text the user enters when typing a different colour when compared to the text that is printed out with System.out.println ?与使用System.out.println打印的文本相比,是否可以使用户在键入不同颜色时输入的文本?

For example if I ran that program in a console that defaults to white text on a black background the following would happen.例如,如果我在默认为黑色背景上的白色文本的控制台中运行该程序,则会发生以下情况。

I would type "hello world" and it would show in red.我会输入“hello world”,它会显示为红色。 When I press enter, it prints "hello world" in the default white.当我按回车键时,它会以默认的白色打印“hello world”。

As a bonus, is it possible to configure a background for the "hello world" when it is printed in red?作为奖励,是否可以为以红色打印的“hello world”配置背景? This means that I can choose two contrasting colours in case a user has their console background at a default setting of red.这意味着我可以选择两种对比色,以防用户的控制台背景默认设置为红色。

Thanks!谢谢!

You can start a windows console by using this code :您可以使用以下代码启动 Windows 控制台:

Process p = Runtime.getRuntime().exec("cmd /c start cmd.exe");
p.waitFor();

Use this code to start your Java-application and set the fontcolor / backgroundcolor of the console.使用此代码启动您的 Java 应用程序并设置控制台的字体颜色/背景颜色。

The /c - switch means that you give the console a command it should run. /c - 开关意味着你给控制台一个它应该运行的命令。 You can test this by clicking on (Windows) Start -> cmd /k color 2c ... this will start a console with some very ugly colors that you can change to suit your needs:您可以通过单击 (Windows) Start -> cmd /k color 2c来测试这一点……这将启动一个带有一些非常难看的颜色的控制台,您可以更改这些颜色以满足您的需要:

https://ss64.com/nt/color.html https://ss64.com/nt/color.html

Then use Sergei's methods to change the color of the Java-output to what you want.然后使用 Sergei 的方法将 Java 输出的颜色更改为您想要的颜色。

Since I did not test this solution, I dont know if the colors will get lost after you've changed them with Sergei's methods!由于我没有测试这个解决方案,我不知道你用 Sergei 的方法改变颜色后颜色会不会丢失! You have to try that.你必须试试。

Some side notes:一些旁注:

  • Why do you want to change the input-colors?为什么要更改输入颜色? This seems to be a lot of effort just for a fancy effect.这似乎只是为了一个花哨的效果而付出了很多努力。
  • You might overwrite the preferences that a user has made on his console!您可能会覆盖用户在其控制台上所做的首选项! For example I use a green font since I can read it better.例如,我使用绿色字体,因为我可以更好地阅读它。 Having a program change this color permanently(?) might upset a user.让程序永久更改此颜色(?)可能会让用户感到不安。
  • This solution only works on windows.此解决方案仅适用于 Windows。 When a user runs it on a linux-based machine, this might fail.当用户在基于 linux 的机器上运行它时,这可能会失败。 So you would have to implement a switch for the different OS like demonstrated here: https://stackoverflow.com/a/5738345/1368690因此,您必须为不同的操作系统实现一个开关,如下所示: https : //stackoverflow.com/a/5738345/1368690

You need to do something like this:你需要做这样的事情:

import java.util.Scanner;

public class App {

    public static final String ANSI_RESET = "\u001B[0m";
    public static final String ANSI_RED = "\u001B[31m";
    public static void main(String[] args) throws Exception {


        System.out.println("Movie Name:" + ANSI_RED);
        Scanner read = new Scanner(System.in);
        String movieName = read.nextLine();
        System.out.println(ANSI_RESET +"\nMovie Genre:" + ANSI_RED);
        

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM