结合使用JTextarea和JTabbedPanel [英] Using JTextarea with 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
一次只有一个值,您需要的是TextArea
的Collection
,例如,您必须对其进行引用在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屋!