【问题标题】:What technology to use to style my Java program?使用什么技术来设置我的 Java 程序的样式?
【发布时间】:2020-01-26 02:00:08
【问题描述】:

我正在尝试创建一个简单的 Java 程序。现在它只需要并显示用户输入。由于我对这种语言很陌生,我想知道如何设计程序以使其看起来对用户更具吸引力?我试图用谷歌搜索它,但我开始认为不可能对 Java 程序进行样式设置...... 我正在使用 Eclipse 工作区并创建了新的 .java 文件

这是我的程序代码:

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

那么,如何设置我的程序的样式?也许我可以以某种方式使用 css 设置样式(例如,为文本添加大小、字体、创建按钮并更改其颜色)?还是其他什么技术?如何加入 java ant 风格?

【问题讨论】:

  • 这个问题可能太宽泛了,有许多不同的库可用于在 Java 中创建 GUI。看到这个post
  • 您会对System.out.print 提供的样式选项感到非常失望。纯 ASCII 文本。没有大小。没有字体。没有按钮。没有颜色。没有CSS。如果您需要任何这些东西,您将需要 System.out.print 以外的其他东西。
  • 你有一个控制台应用程序,它是严格基于文本的,外观取决于你在其中运行它的控制台。也许你需要自己找一个关于创建图形用户界面(GUI ) 在 Java 中。
  • 谢谢,会检查你给我的建议。但没有必要否决我的问题。我只是在寻求帮助,因为即使我今天找了 5 个小时,我也无法在互联网上找到它。所以它可能对其他人也有用。谢谢
  • 它被否决了,因为它与本网站无关。它太宽泛了,并且要求工具/框架推荐和/或教程,所有这些都是离题的,请参阅help center 了解详细信息。

标签: java styles


【解决方案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);

【讨论】:

    猜你喜欢
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    相关资源
    最近更新 更多