【问题标题】:Java JTextArea multiline helpJava JTextArea 多行帮助
【发布时间】:2011-10-06 07:27:51
【问题描述】:

我遇到的一个问题是我有 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);

}

任何关于我可以做不同或改变的帮助将不胜感激

【问题讨论】:

    标签: java swing newline substring jtextarea


    【解决方案1】:

    JTextAreasetLineWrap(...)setWrapStyleWord(...) 方法。也许您需要做的就是在您的JTextArea 的设置都为true 时调用这些。

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

    【讨论】:

    • @Michael:如果有帮助,则对答案进行投票,如果解决了问题,则“接受”答案。不要编辑您的问题标题以将其声明为“已关闭”,因为这在此论坛中具有完全不同的含义。这表明论坛成员以问题不充分或不适当而结束。
    【解决方案2】:

    也许更好的解决方案是使用 JList。见How to Use Lists

    您发布的代码不完整。如果您仍想使用文本区域解决方案,请发布您的 SSCCE 以说明问题。

    【讨论】:

    • JList 错过了很多方法来与只有一个 TableColumn 而没有 TableHeader +1 的 JTable 进行比较
    • @Michael:camickr 的解决方案可能是更好的解决方案(1+)。你最好去看看。
    猜你喜欢
    • 2015-11-14
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多