我正在制作一个基于文本" shop"在大多数GUI中显示项目的位置。像这样:
__________________________________________________
| Item 1 | Item 2 | Item 3 |
| Item : price | Item:price | Item:price |
| Item weight | item weight | item weight |
---------------------------------------------------
| item 4 | item 5 | item 6 |
---------------------------------------------------
所有创建的项目都使用相同的打印功能,除了剑也有dmg打印功能,但没什么特别的。
public void print()
{
System.out.println("#_____________________________________");
System.out.println("# -Name of item: " + itemName + "-");
System.out.println("# Item description: " + desc);
System.out.println("# Item value: " + value + " gold");
System.out.println("# Item weight: " + weight + "kg");
System.out.println("# Item action: " + action);
}
他们从其他地方获得价值,现在我明白我不能只做print(),print(),print();他们会彼此相邻。我主要担心的是每次创建项目在完全相同的位置显示的框,并且所有文本都包含在框内部,因为描述有时相当长。我确实有一个描述修改类,我可能会用它来使其适合。我从其他人那里得到了替换部分,所以我对#34;编码"不太熟悉。那个。
public String formatDesc(String desc)
{
desc = "\n#\t" + desc;
desc += "\n"; // Needed to handle last line correctly
desc = desc.replaceAll("(.{1,30})\\s+", "$1\n#\t");
return desc;
}
答案 0 :(得分:1)
为了让盒子放置正确,我会找出每列中所有项目的最大长度。如果我现在打印item1,我会查看打印字符串的长度,并计算出最多的缺失空格,以便:
int max_column1=//initialized via maximum length all items in each column
int difference=max_column1-print().length()
并为这种差异打印空间。之后是'|'
或者您的formatDesc()
方法可以在指定的最大列宽处进行换行。