Java JTextArea多行帮助

前端之家收集整理的这篇文章主要介绍了Java JTextArea多行帮助前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我遇到的一个问题是我有2个JTextAreas,我需要为它们添加一个项目列表.
我遇到的问题是当字符串到达​​JTextArea的末尾时,它不会自动移动到下一行.所以为了解决这个问题我尝试了这个:(对不起,如果我的代码有点草率.)
public void setIncludeAndExclude(ArrayList<JComboBox> Boxes){
    String in = "",ex = "";
    String[] inSplit,exSplit;


    boolean[] include = new boolean[Boxes.get(0).getModel().getSize()-1];
    for(int i = 0; i < Boxes.size(); i ++){
        if(Boxes.get(i).getSelectedIndex() != 0){
            include[Boxes.get(i).getSelectedIndex() -1] = true;
        }
    }

    for(int i = 0; i < include.length; i ++){
        if(include[i]){
            //numToItem is a method that turns an int into a string e.g. 1 = "Acesss Doors"
            in += (numToItem(i+1)+ ",");
        }else{
            ex += (numToItem(i+1)+ ",");
        }
    }

    //take off the last comma
    in = in.substring(0,in.lastIndexOf(","));
    ex = ex.substring(0,ex.lastIndexOf(","));

    //get how many lines there should be        
    inSplit = new String[(in.length()/100) +1];
    exSplit = new String[(ex.length()/100) +1];

    String temp;        
    int istart = 0,iend = Math.min(100,in.length()),estart = 0,eend = Math.min(100,ex.length());

    for(int i = 0; i < inSplit.length; i ++){
        try{
            temp = in.substring(istart,iend);
            int Iindex = temp.lastIndexOf(",");
            temp = ex.substring(estart,eend);
            int Eindex = temp.lastIndexOf(",");
            inSplit[i] = in.substring(istart,Iindex);
            exSplit[i] = ex.substring(estart,Eindex);
            istart = Iindex; iend = Math.min(iend + 100,in.length());
            estart = Eindex; eend = Math.min(eend + 100,ex.length());
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    //reset in and ex to ""     
    in = ""; ex = "";

    //set in and ex to the new string with newline characters
    for(int i = 0; i < inSplit.length; i ++){
        in += inSplit[i] + "\n";
        ex += exSplit[i] + "\n";
    }

    //set the text of the JTextAreas
    Include.setText(in);
    Exclude.setText(ex);

}

任何帮助我可以做的不同或改变将非常感激

解决方法

JTextArea具有setLineWrap(…)和setWrapStyleWord(…)方法.也许您需要做的就是在JTextArea的设置上将它们都设置为true.

一点批评:你的代码很难解释,因为你没有说明哪些变量是JTextAreas(我猜是“包括”和“排除”),并没有评论什么是做什么.请在这里写下你的问题,我们的想法是我们对你的代码一无所知,也无法读懂思想.你的问题越清楚,通常就越容易回答.谢谢.

猜你在找的Java相关文章