Java 如果当前行包含太多文本,是否在Stringlist中创建新行?
所以我现在有我的stringlist,用户可以在其中编辑输入的内容,只要它是一个字符串。因此,对于较短的文本,这很好,但是当文本开始变得越来越长时,我使用stringlist的工具提示将占用屏幕太多的空间,使事情看起来很难看。所以我想为每一个特定数量的字符创建一个新的字符串列表,这样它就不会变得混乱。以下是我目前正在进行的测试:Java 如果当前行包含太多文本,是否在Stringlist中创建新行?,java,api,minecraft,bukkit,Java,Api,Minecraft,Bukkit,所以我现在有我的stringlist,用户可以在其中编辑输入的内容,只要它是一个字符串。因此,对于较短的文本,这很好,但是当文本开始变得越来越长时,我使用stringlist的工具提示将占用屏幕太多的空间,使事情看起来很难看。所以我想为每一个特定数量的字符创建一个新的字符串列表,这样它就不会变得混乱。以下是我目前正在进行的测试: 'Salamander specializes in disguising in the green woods, mainly in oak forests sala
'Salamander specializes in disguising in the green woods, mainly in oak forests salamanders will hide and lure to wait for your loot, they will then jump out and do a sneak attack. Be prepared!'
我通过以下方式获得该信息:
lore.add(kitConfig.getString("kits." + kitName + ".description"));
lore
ArrayList
中的每个字符串都显示在新行上。您可以编写一个方法,将字符串
拆分为段,并返回一个数组列表
,其中包含可以设置为新知识的部分。下面是一个示例,展示了它的外观:
public static ArrayList<String> splitLore(String text, int characters) {
ArrayList<String> lore = new ArrayList<String>(); // Create the ArrayList that will contain the lore lines
if (text.length() <= characters) { // If the line of text is short enough (doesn't need to be split)...
lore.add(text); // Add the entire line to the list and return it
} else { // If the line is longer and needs to be split into at least two lines...
int beginIndex = 0; // A "begin index" where each substring or segment will begin
while (beginIndex <= text.length() - 1) { // If the index is not larger than the last character index in the line of text...
lore.add(text.substring(beginIndex, Math.min(beginIndex + characters, text.length()))); // Add the segment
beginIndex += characters;
// This will also add any trailing segments at the end of the line that are shorter than the character limit
}
}
return lore;
}
这种方法不是很“聪明”,会将单词切掉,产生看起来不太漂亮或难以阅读的文本行
为了让这些知识更容易阅读,你可以把一行文字分成“单词”,然后试着把它们分组,这样就不会有任何一行文字超过“软”字的限制。下面的示例方法不会分割单词,尽管它也不会强制分割占用超过整行的超长单词
public static ArrayList<String> splitLoreNicely(String text, int characters) {
ArrayList<String> lore = new ArrayList<>();
String[] words = text.split(" "); // Get the "words" in the line of text by splitting the space characters
int wordsUsed = 0; // A counter for how many words have been placed in lines so far
while (wordsUsed < words.length) { // Repeat this process until all words have been placed into separate lines
String line = ""; // The line that will be added to the lore list
for (int i = wordsUsed; i < words.length; i++) { // For each remaining word in the array
if (line.length() + words[i].length() >= characters) { // If adding the next word exceeds or matches the character limit...
line += words[i]; // Add the last word in the line without a space character
wordsUsed++;
break; // Break out of this inner loop, since we have reached/exceeded the character limit for this line
} else { // If adding this word does not exceed or match the character limit...
line += words[i] + " "; // Add the word with a space character, continue for loop
wordsUsed++;
}
}
lore.add(line); // Add the line of text to the list
}
return lore;
}
publicstaticarraylistsplitlorenicely(字符串文本,int字符){
ArrayList lore=新的ArrayList();
String[]words=text.split(“”;//通过拆分空格字符获取文本行中的“words”
int wordsUsed=0;//一个计数器,用于计算到目前为止已在行中放置了多少个单词
while(wordsUsed=个字符){//如果添加下一个单词超过或匹配字符限制。。。
line+=单词[i];//将最后一个单词添加到行中,不带空格字符
wordsUsed++;
break;//中断此内部循环,因为我们已达到/超过此行的字符限制
}否则{//如果添加此单词未超过或匹配字符限制。。。
line+=words[i]+“”;//添加带空格字符的单词,继续循环
wordsUsed++;
}
}
lore.add(line);//将文本行添加到列表中
}
回归知识;
}
谢谢,我会试试的。
public static ArrayList<String> splitLoreNicely(String text, int characters) {
ArrayList<String> lore = new ArrayList<>();
String[] words = text.split(" "); // Get the "words" in the line of text by splitting the space characters
int wordsUsed = 0; // A counter for how many words have been placed in lines so far
while (wordsUsed < words.length) { // Repeat this process until all words have been placed into separate lines
String line = ""; // The line that will be added to the lore list
for (int i = wordsUsed; i < words.length; i++) { // For each remaining word in the array
if (line.length() + words[i].length() >= characters) { // If adding the next word exceeds or matches the character limit...
line += words[i]; // Add the last word in the line without a space character
wordsUsed++;
break; // Break out of this inner loop, since we have reached/exceeded the character limit for this line
} else { // If adding this word does not exceed or match the character limit...
line += words[i] + " "; // Add the word with a space character, continue for loop
wordsUsed++;
}
}
lore.add(line); // Add the line of text to the list
}
return lore;
}