结合使用JTextarea和JTabbedPanel [英] Using JTextarea with JTabbedPanel

查看:89
本文介绍了结合使用JTextarea和JTabbedPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JTextarea是动态创建的,并使用以下代码添加到jTabbed面板中:

JTextarea is dynamically created and added to the jTabbed panel using the code:

            // tabidis is a variable with unique value in each case

            JScrollPane panel2 = new JScrollPane();
            panel2.setName(tabidis);

            ta = new JTextArea("");
            ta.setColumns(30);
            ta.setRows(20);
            ta.setEditable(false);
            panel2.setViewportView(ta);
            ta.setName(tabidis);

            jTabbedPane1.add(username4, panel2);

添加新选项卡(同时添加一个文本区域)时,最后一个选项卡文本区域将接收所有文本.

When new tabs are added (ta textarea is added along with it), the last tabs textarea recieves all the text.

 private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt){
                send3 = ta.getName();
                ta.setName(send3);
                ta.setText(ta.getText()+send3);
                }

在上面的代码中,您可以看到应该更新两个文本区域中的文本(在两个选项卡中).但是真正发生的是,只有第二个TextArea正在更新.第一个TextArea没有更新.

In the above code you can see that The text in both the textareas(In two tabs) should be updated. But what really happens is that only the second TextArea is getting updated.The first TextArea is not updated.

推荐答案

ta一次只有一个值,您需要的是TextAreaCollection,例如,您必须对其进行引用在List<JTextArea> textAreas

ta only have a value at a time, what you need is a Collection of TextArea you have to have a reference to them for example in a List<JTextArea> textAreas

然后在您的代码中

        JTextArea ta = new JTextArea("");
        ta.setColumns(30);
        ta.setRows(20);
        ta.setEditable(false);
        textAreas.add(ta);

在您的情况下,类似以下内容:

And in your event something like this:

private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt){
                for(JTextArea ta : textAreas ){
                 send3 = ta.getName(); // this line an below are redundant
                 ta.setName(send3);
                 ta.setText(ta.getText()+send3);
               }
}

这篇关于结合使用JTextarea和JTabbedPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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