怎么樣用System.out.println在控制臺打印出顏色

問題:怎么樣用System.out.println在控制臺打印出顏色

怎么樣才能在控制臺里打印顏色啊?我想要展示一些有顏色的字體,當處理器發送數據和接收數據的時候,也使用不同顏色的字體。

回答一

在這個Java類里面帶有public static 的數據域里面有一系列的顏色。

用法

System.out.println(ConsoleColors.RED + "RED COLORED" +
ConsoleColors.RESET + " NORMAL");

記住千萬不要忘記在打印以后重置,沒有清理的話,那個效果會一直保留的。

public class ConsoleColors {// Resetpublic static final String RESET = "\033[0m";  // Text Reset// Regular Colorspublic static final String BLACK = "\033[0;30m";   // BLACKpublic static final String RED = "\033[0;31m";     // REDpublic static final String GREEN = "\033[0;32m";   // GREENpublic static final String YELLOW = "\033[0;33m";  // YELLOWpublic static final String BLUE = "\033[0;34m";    // BLUEpublic static final String PURPLE = "\033[0;35m";  // PURPLEpublic static final String CYAN = "\033[0;36m";    // CYANpublic static final String WHITE = "\033[0;37m";   // WHITE// Boldpublic static final String BLACK_BOLD = "\033[1;30m";  // BLACKpublic static final String RED_BOLD = "\033[1;31m";    // REDpublic static final String GREEN_BOLD = "\033[1;32m";  // GREENpublic static final String YELLOW_BOLD = "\033[1;33m"; // YELLOWpublic static final String BLUE_BOLD = "\033[1;34m";   // BLUEpublic static final String PURPLE_BOLD = "\033[1;35m"; // PURPLEpublic static final String CYAN_BOLD = "\033[1;36m";   // CYANpublic static final String WHITE_BOLD = "\033[1;37m";  // WHITE// Underlinepublic static final String BLACK_UNDERLINED = "\033[4;30m";  // BLACKpublic static final String RED_UNDERLINED = "\033[4;31m";    // REDpublic static final String GREEN_UNDERLINED = "\033[4;32m";  // GREENpublic static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOWpublic static final String BLUE_UNDERLINED = "\033[4;34m";   // BLUEpublic static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLEpublic static final String CYAN_UNDERLINED = "\033[4;36m";   // CYANpublic static final String WHITE_UNDERLINED = "\033[4;37m";  // WHITE// Backgroundpublic static final String BLACK_BACKGROUND = "\033[40m";  // BLACKpublic static final String RED_BACKGROUND = "\033[41m";    // REDpublic static final String GREEN_BACKGROUND = "\033[42m";  // GREENpublic static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOWpublic static final String BLUE_BACKGROUND = "\033[44m";   // BLUEpublic static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLEpublic static final String CYAN_BACKGROUND = "\033[46m";   // CYANpublic static final String WHITE_BACKGROUND = "\033[47m";  // WHITE// High Intensitypublic static final String BLACK_BRIGHT = "\033[0;90m";  // BLACKpublic static final String RED_BRIGHT = "\033[0;91m";    // REDpublic static final String GREEN_BRIGHT = "\033[0;92m";  // GREENpublic static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOWpublic static final String BLUE_BRIGHT = "\033[0;94m";   // BLUEpublic static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLEpublic static final String CYAN_BRIGHT = "\033[0;96m";   // CYANpublic static final String WHITE_BRIGHT = "\033[0;97m";  // WHITE// Bold High Intensitypublic static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACKpublic static final String RED_BOLD_BRIGHT = "\033[1;91m";   // REDpublic static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREENpublic static final String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOWpublic static final String BLUE_BOLD_BRIGHT = "\033[1;94m";  // BLUEpublic static final String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLEpublic static final String CYAN_BOLD_BRIGHT = "\033[1;96m";  // CYANpublic static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE// High Intensity backgroundspublic static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACKpublic static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// REDpublic static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREENpublic static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOWpublic static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUEpublic static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLEpublic static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m";  // CYANpublic static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m";   // WHITE}

回答二

使用顏色函數打印帶有顏色的文本

代碼:

enum Color {RED("\033[0;31m"),      // REDGREEN("\033[0;32m"),    // GREENYELLOW("\033[0;33m"),   // YELLOWBLUE("\033[0;34m"),     // BLUEMAGENTA("\033[0;35m"),  // MAGENTACYAN("\033[0;36m"),     // CYANprivate final String codeColor(String code) {this.code = code;}@OverrideString toString() {return code}
}def color = { color, txt ->def RESET_COLOR = "\033[0m"return "${color}${txt}${RESET_COLOR}"
}

用法:

test {println color(Color.CYAN, 'testing')
}

文章翻譯自Stack Overflow:https://stackoverflow.com/questions/5762491/how-to-print-color-in-console-using-system-out-println

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390950.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390950.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390950.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

sql注入語句示例大全_SQL Order By語句:示例語法

sql注入語句示例大全Order By is a SQL command that lets you sort the resulting output from a SQL query.Order By是一個SQL命令,可讓您對SQL查詢的結果輸出進行排序。 訂購依據(ASC,DESC) (Order By (ASC, DESC)) ORDER BY gives us a way to SORT…

[BZOJ2599][IOI2011]Race 點分治

2599: [IOI2011]Race Time Limit: 70 Sec Memory Limit: 128 MBSubmit: 3934 Solved: 1163[Submit][Status][Discuss]Description 給一棵樹,每條邊有權.求一條簡單路徑,權值和等于K,且邊的數量最小.N < 200000, K < 1000000 Input 第一行 兩個整數 n, k第二..n行 每行三…

分詞消除歧義_角色標題消除歧義

分詞消除歧義折磨數據&#xff0c;它將承認任何事情 (Torture the data, and it will confess to anything) Disambiguation as defined in the vocabulary.com dictionary refers to the removal of ambiguity by making something clear and narrowing down its meaning. Whi…

北航教授李波:說AI會有低潮就是胡扯,這是人類長期的追求

這一輪所謂人工智能的高潮&#xff0c;和以往的幾次都有所不同&#xff0c;那是因為其受到了產業界的極大關注和參與。而以前并不是這樣。 當今世界是一個高度信息化的世界&#xff0c;甚至我們有一只腳已經踏入了智能化時代。而在我們日常交流和信息互動中&#xff0c;迅速發…

創建字符串枚舉的最好方法

問題&#xff1a;創建字符串枚舉的最好方法 用一個枚舉類型去表示一組字符串的最好方法是什么 我嘗試這樣&#xff1a; enum Strings{STRING_ONE("ONE"), STRING_TWO("TWO") }我怎么樣才可以像使用字符串那樣使用它們&#xff1f; 回答一 我不知道你想…

網絡安全習慣_健康習慣,確保良好的網絡安全

網絡安全習慣In a similar fashion to everyone getting the flu now and again, the risk of catching a cyberattack is a common one. Both a sophisticated social engineering attack or grammatically-lacking email phishing scam can cause real damage. No one who c…

attr和prop的區別

由于prop(property的縮寫)和attr(attribute的縮寫)翻譯成漢語&#xff0c;均有“特性、屬性”等意思的原因&#xff0c;導致大家容易混淆分不清。 (1)在處理自定義時屬性時&#xff0c;用attr()&#xff0c;若用prop(),則結果為undefined&#xff1b; (2)DOM固有屬性&#xff0…

15行Python代碼,幫你理解令牌桶算法

在網絡中傳輸數據時&#xff0c;為了防止網絡擁塞&#xff0c;需限制流出網絡的流量&#xff0c;使流量以比較均勻的速度向外發送&#xff0c;令牌桶算法就實現了這個功能&#xff0c;可控制發送到網絡上數據的數目&#xff0c;并允許突發數據的發送。 什么是令牌 從名字上看令…

在Java中,如何使一個字符串的首字母變為大寫

問題&#xff1a;在Java中&#xff0c;如何使一個字符串的首字母變為大寫 我使用Java去獲取用戶的字符串輸入。我嘗試使他們輸入的第一個字符大寫 我嘗試這樣: String name;BufferedReader br new InputStreamReader(System.in);String s1 name.charAt(0).toUppercase());…

在加利福尼亞州投資于新餐館:一種數據驅動的方法

“It is difficult to make predictions, especially about the future.”“很難做出預測&#xff0c;尤其是對未來的預測。” ~Niels Bohr?尼爾斯波爾 Everything is better interpreted through data. And data-driven decision making is crucial for success in any ind…

javascript腳本_使用腳本src屬性將JavaScript鏈接到HTML

javascript腳本The ‘src’ attribute in a tag is the path to an external file or resource that you want to link to your HTML document.標記中的src屬性是您要鏈接到HTML文檔的外部文件或資源的路徑。 For example, if you had your own custom JavaScript file named …

阿里云ESC上的Ubuntu圖形界面的安裝

系統裝的是Ubuntu Server 16.04 64位版的圖形界面&#xff0c;這里是轉載的一個大神的帖子 http://blog.csdn.net/dk_0228/article/details/54571867&#xff0c; 當然自己也再記錄一下&#xff0c;加深點印象 1.更新apt-get 保證最新 apt-get update 2.用putty或者Xshell連接遠…

leetcode 1269. 停在原地的方案數(dp)

示例 1&#xff1a; 輸入&#xff1a;steps 3, arrLen 2 輸出&#xff1a;4 解釋&#xff1a;3 步后&#xff0c;總共有 4 種不同的方法可以停在索引 0 處。 向右&#xff0c;向左&#xff0c;不動 不動&#xff0c;向右&#xff0c;向左 向右&#xff0c;不動&#xff0c;向…

JavaScript Onclick事件解釋

The onclick event in JavaScript lets you as a programmer execute a function when an element is clicked.JavaScript中的onclick事件可讓您作為程序員在單擊元素時執行功能。 按鈕Onclick示例 (Button Onclick Example) <button onclick"myFunction()">C…

近似算法的近似率_選擇最佳近似最近算法的數據科學家指南

近似算法的近似率by Braden Riggs and George Williams (gwilliamsgsitechnology.com)Braden Riggs和George Williams(gwilliamsgsitechnology.com) Whether you are new to the field of data science or a seasoned veteran, you have likely come into contact with the te…

VMware安裝CentOS之二——最小化安裝CentOS

1、上文已經創建了一個虛擬機&#xff0c;現在我們點擊開啟虛擬機。2、虛擬機進入到安裝的界面&#xff0c;在這里我們選擇第一行&#xff0c;安裝或者升級系統。3、這里會提示要檢查光盤&#xff0c;我們直接選擇跳過。4、這里會提示我的硬件設備不被支持&#xff0c;點擊OK&a…

什么是GraphQL? 普通神話被揭穿。

I love talking about GraphQL, especially with people who have been working with GraphQL or thinking of adopting GraphQL. One common question people have is why someone would want to move to GraphQL from REST. 我喜歡談論GraphQL&#xff0c;特別是和那些一直在…

在Spring Boot里面,怎么獲取定義在application.properties文件里的值

問題&#xff1a;在Spring Boot里面&#xff0c;怎么獲取定義在application.properties文件里的值、 我想訪問application.properties里面提供的值&#xff0c;像這樣&#xff1a; logging.level.org.springframework.web: DEBUG logging.level.org.hibernate: ERROR logging…

連接sqlexpress

sqlexpress在visualstudio安裝時可選擇安裝。   數據源添加 localhost\sqlexpress window身份認證即可。轉載于:https://www.cnblogs.com/zjxbetter/p/7767241.html

在Python中使用Seaborn和WordCloud可視化YouTube視頻

I am an avid Youtube user and love watching videos on it in my free time. I decided to do some exploratory data analysis on the youtube videos streamed in the US. I found the dataset on the Kaggle on this link我是YouTube的狂熱用戶&#xff0c;喜歡在業余時間…