在JTextArea中的列中对齐字符串 [英] Align Strings in columns in JTextArea

查看:98
本文介绍了在JTextArea中的列中对齐字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JTextArea中打印字符串并正确对齐它们。很难解释所以我会上传我想要实现的屏幕截图。

I want to print Strings in JTextArea and align them properly. Its hard to explain so I will upload the screen shot of what I am trying to achieve.

因此,每行打印的字符串都是从Paper对象打印出来的,该对象具有参数(id ,标题,作者,日期,等级)。数据从文本文件中读取,并使用loadPaper()函数存储在LinkedList中。

So Strings printed in each line are printed from Paper object which has parameters (id, title, author, date, rank). The data is read from a text file and is stored in a LinkedList using loadPaper() function.

然后displayPapers()函数用于显示Paper对象的内容JTextArea。
displayPapers()如下所示:

Then displayPapers() function is used to display content of the Paper object to the JTextArea. displayPapers() is listed below:

/** Print all Paper object present in the LinkedList paperList to textArea */
public void displayPapers(){
    // clear textArea before displaying new content
    displayTxtArea.setText("");

    Paper currentPaper;
    ListIterator<Paper> iter = paperList.listIterator();

    while(iter.hasNext()){
        currentPaper = iter.next();
        String line = currentPaper.toString();

        if("".equals(line)){
            continue;
        } // end if

        String[] words = line.split(",");
        displayTxtArea.append   ("  " 
                                + padString(words[0],30) 
                                + padString(words[1],30) 
                                + "    " 
                                + padString(words[2],30) 
                                + "  " 
                                + padString(words[3],30)  
                                + padString(words[4],30) 
                                + "\n");

        System.out.println(words);
        //displayTxtArea.append(currentPaper.toString());
    } // end while

    displayTxtArea.append("  Total " + noOfPapers + " entries!");

} // end showAllPaper

padString()函数添加空格字符串,以便它们都具有相同数量的单词。 PadString()如下所示:

The padString() function adds spaces to the String so that all of them have same number of words. PadString() is listed below:

/** Add spaces to Strings so that all of the are of same number of characters
 *  @param str  String to be padded
 *  @param n    total number words String should be padded to
 * @return str  Padded string 
 */
private String padString(String str, int n){
    if(str.length() < n){
        for(int j = str.length(); j < n; j++){
            str += " ";
        } // end for
    } // end if
    return str;
} // end padString

我已经工作了一段时间但仍然无法获得解决方案。正如您可以注意到上面的图片并非所有内容都按照预期完全对齐。

I have worked on this for a while but still cant get the solution. As you can notice the above picture not everything is perfectly aligned as intended.

如何完美地对齐它们以使它看起来更好?谢谢。

How do I align them perfectly so that it looks nicer? Thanks.

推荐答案

只有在使用单倍间距字体时,输出才会在JTextArea中正确对齐。例如,Andale Mono 14就可以解决这个问题。

Output will be aligned "properly" in your JTextArea only if you use a mono-spaced font. "Andale Mono 14" for example would do the trick.

此外,为了让您的生活更轻松并避免填充地狱,请使用 String.format ,其中语法

Also, in order to make your life easier and avoid the padding hell, use String.format with it's syntax.

String format = "%1$5s %2$-40s %3$-20s";
String someLine;
while (whatEver...) {
   ... 
   someLine = String.format(format, aNum, aName, aDate);
   jTextArea1.append(someLine + "\n");
}

这篇关于在JTextArea中的列中对齐字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆