Java JTextArea 多行帮助

发布于 2024-11-24 20:03:11 字数 2153 浏览 2 评论 0原文

我遇到的一个问题是我有 2 个 JTextArea,我需要向它们添加一个项目列表。 我遇到的问题是字符串到达​​ 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);

}

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

one problem I'm having is i have 2 JTextAreas and i need to add a list of items to them.
The problem I'm running into is the string doesn't automatically move to the next line when it reaches the end of the JTextArea. So to solve this problem I tried this: (sorry if my code is kinda sloppy.)

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);

}

any help on what i could do different or change would be much appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳若烟 2024-12-01 20:03:11

JTextArea 具有 setLineWrap(...)setWrapStyleWord(...) 方法。也许您需要做的就是在 JTextArea 的设置中调用它们并将两者设置为 true。

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

JTextArea has setLineWrap(...) and the setWrapStyleWord(...) methods. Perhaps all you need to do is call these on your JTextArea's setting both to true.

One bit of criticism: your code is very hard to interpret as you give no indication which variables are JTextAreas (which I'm guessing are "Include" and "Exclude"), and no comments as to what is doing what. Please write your questions here with the idea that we know nothing about your code and can't read minds. The clearer your question, usually the easier it is to answer. Thanks.

稍尽春風 2024-12-01 20:03:11

也许更好的解决方案是使用 JList。请参阅如何使用列表

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

Maybe a better solution is to use JList. See How to Use Lists.

The code you posted is not complete. If you still want to use a text area solution then post your SSCCE that demonstrates the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文