Java Console/控制台 打印表格


功能:控制台打印表格,支持字段動態長度,左對齊,右對齊,居中,設置最大列長,設置列間隔符,設置最多打印多少行。

類下載地址:http://download.csdn.net/download/j506825719/10211082

簡單使用方法:在new的時候,給定列名,和列值List,之后調用printTable就得到了表格字符串。

進階使用方法:查看所有public方法,就能知道還有什么功能了。

效果圖如下:


接口如下:


   
   
  
  
          
  1. package com.benjamin.debug.extendclass;
  2. import java.awt.Font;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util. List;
  6. import java.util.Map;
  7. /**
  8. *
  9. * @author Benjamin su
  10. * QQ:506825719
  11. * Email:jiang506825719@qq.com
  12. *
  13. */
  14. public class TextTable {
  15. //這個變量只是用來獲取字符串的標准長度,修改無意義
  16. private static Font font = new Font( "宋體", Font.PLAIN, 16);
  17. private String columnInterval= "|"; //列之間的間隔符號
  18. private AlignType at=AlignType.Left;
  19. private Integer MaxValueLenth= 40;
  20. private Map<String,Integer> maxFieldValueLengthMap= null;
  21. List<String> columnNameList= null;
  22. List< List<String>> columnValueList= null;
  23. private boolean isException= false;
  24. private Integer pringtTableRow= 30;
  25. /**
  26. * 初始化時不給定列長度Map
  27. * @param columnNameListTmp
  28. * @param columnValueListTmp
  29. */
  30. public TextTable( List<String> columnNameListTmp, List< List<String>> columnValueListTmp){
  31. }
  32. /**
  33. * 初始化時給定列長度Map
  34. * @param columnNameListTmp
  35. * @param columnValueListTmp
  36. * @param maxFieldValueLengthMapTmp
  37. */
  38. public TextTable( List<String> columnNameListTmp, List< List<String>> columnValueListTmp,Map<String,Integer> maxFieldValueLengthMapTmp){
  39. }
  40. /**
  41. * 獲得處理后的表格字符串
  42. * @return
  43. */
  44. public String printTable(){
  45. }
  46. /**
  47. * 獲取一個字符串的標准長度是多少
  48. * A2*. 這樣是四個標准長度
  49. * 系統系統 這樣是8個標准長度
  50. * @param str
  51. * @return
  52. */
  53. public static Integer getStrPixelsLenth(String str){
  54. }
  55. /**
  56. * 讓列值居中
  57. * @param columnValue
  58. * @param ColumnPixelsLenth
  59. * @return
  60. */
  61. private String makeValueCenter(String columnValue,Integer repaireStrLength){
  62. }
  63. /**
  64. * 讓列值左對齊
  65. * @param columnValue
  66. * @param ColumnPixelsLenth
  67. * @return
  68. */
  69. private String makeValueLeft(String columnValue,Integer repaireStrLength){
  70. }
  71. /**
  72. * 讓列值右對齊
  73. * @param columnValue
  74. * @param ColumnPixelsLenth
  75. * @return
  76. */
  77. private String makeValueRight(String columnValue,Integer repaireStrLength){
  78. }
  79. /**
  80. * 獲取用於填充的字符串
  81. * @param str
  82. * @param PixelsLenth
  83. * @return
  84. */
  85. private static String getRepairStr(char str,Integer PixelsLenth){
  86. }
  87. /**
  88. * 根據像素長度來切割字符串
  89. * @param str
  90. * @param PixelsLenth
  91. * @return
  92. */
  93. private static String subStrByPixels(String str,Integer PixelsLenth){
  94. }
  95. /**
  96. * 讓列值對齊(左對齊OR居中OR右對齊)
  97. * @param columnValue
  98. * @param ColumnPixelsLenth
  99. * @return
  100. */
  101. private String makeValueAlign(String columnValue,Integer ColumnPixelsLenth){
  102. }
  103. /**
  104. * 設置列與列之間用什么間隔,默認是|
  105. * @param columnInterval
  106. */
  107. public void setColumnInterval(String columnInterval) {
  108. }
  109. /**
  110. * 設置列值最大長度,超過則用省略號代替
  111. * @param maxValueLenth
  112. */
  113. public void setMaxValueLenth(Integer maxValueLenth) {
  114. }
  115. /**
  116. * 如果不指定列長度Map,這里將自動計算
  117. * @param columnNameListTmp
  118. * @param columnValueListTmp
  119. * @param maxFieldValueLengthMapTmp
  120. */
  121. private void setMaxFieldValueLengthMap( List<String> columnNameListTmp, List< List<String>> columnValueListTmp,Map<String, Integer> maxFieldValueLengthMapTmp) {
  122. }
  123. /**
  124. * 設置最多打印多少行
  125. * @param pringtTableRow
  126. */
  127. public void setPringtTableRow(Integer pringtTableRow) {
  128. }
  129. /**
  130. * 設置對齊方式
  131. * @param at
  132. */
  133. public void setAt(AlignType at) {
  134. }
  135. public enum AlignType {
  136. Left,
  137. Center,
  138. Rigth
  139. }
  140. }



原文地址:https://blog.csdn.net/j506825719/article/details/78996659


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM