将 ArrayList 内容打印到 TextArea
Printing ArrayList contents into a TextArea
我有两个 ArrayList,名为 keys 和 values。
keys = {"cat", "dog", "mouse"}
values = {"furry;white", "greyhound;beast", "little;rat"}
我想将它们打印在 JTextArea 中(因为我希望它们是可编辑的)或像这样的任何其他 GUI 组件:
------------------
cat | furry
| white
-------------------
dog | greyhound
| beast
-------------------
mouse | little
| rat
我应该为每个条目动态创建一个新的 TextArea 吗?如果是这样,我该怎么做?这是正确的做法吗?
我已经尝试过使用 JTable,但问题是单元格高度是固定的,文本换行一团糟。
我建议为每个可编辑字段设置单独的 JTextField
实例,然后使用 GridLayout
对它们进行布局。您的代码最终会像这种方法一样创建一个包含字段的面板。
public JPanel createPanelForStringMap(Map<String, String> map) {
JPanel panel = new JPanel(new GridLayout(map.size(), 2));
for (Map.Entry entry: map.entrySet()) {
panel.add(new JTextField(entry.getKey()));
panel.add(new JTextField(entry.getValue()));
}
return panel;
}
当然,您需要添加代码来对编辑后的值执行某些操作。
您可以使用 GridLayout
Map<String,String> map=new HashMap<String,String>(); //key-Value pair
map.put("cat",""furry\n white"); // \n So that you will get a new line character in text area
map.put("dog","greyhound \n beast");
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(rows,columns));//In your case (map.size,2)
frame.setSize(300, 300);
//Now You need to Iterate through the Map.
for(Map.Entry<String,String>entry:map.entrySet()){
JTextArea left=JTextArea(entry.getKey());
JTextArea right=JTextArea(entry.getValue());
frame.add(left); //Adding each key to the Frame on left side
frame.add(right); //This is the value you want in side of key
}
frame.setVisible(true);
你会得到类似这样的东西,但格式是可编辑的
I have two ArrayLists called keys and values.
keys = {"cat", "dog", "mouse"}
values = {"furry;white", "greyhound;beast", "little;rat"}
如您所见,您正在单独维护键和值列表,这是一种不好的做法,您可以使用 Map(只需阅读此 link,它会有所帮助)存储key
和它对应的 value
在一条记录中,
现在提一点建议
如果你想对一个key
使用multiple values
,我建议你使用MultiMap,它针对一个键维护多个值,这是非常有用的库。
我有两个 ArrayList,名为 keys 和 values。
keys = {"cat", "dog", "mouse"}
values = {"furry;white", "greyhound;beast", "little;rat"}
我想将它们打印在 JTextArea 中(因为我希望它们是可编辑的)或像这样的任何其他 GUI 组件:
------------------
cat | furry
| white
-------------------
dog | greyhound
| beast
-------------------
mouse | little
| rat
我应该为每个条目动态创建一个新的 TextArea 吗?如果是这样,我该怎么做?这是正确的做法吗?
我已经尝试过使用 JTable,但问题是单元格高度是固定的,文本换行一团糟。
我建议为每个可编辑字段设置单独的 JTextField
实例,然后使用 GridLayout
对它们进行布局。您的代码最终会像这种方法一样创建一个包含字段的面板。
public JPanel createPanelForStringMap(Map<String, String> map) {
JPanel panel = new JPanel(new GridLayout(map.size(), 2));
for (Map.Entry entry: map.entrySet()) {
panel.add(new JTextField(entry.getKey()));
panel.add(new JTextField(entry.getValue()));
}
return panel;
}
当然,您需要添加代码来对编辑后的值执行某些操作。
您可以使用 GridLayout
Map<String,String> map=new HashMap<String,String>(); //key-Value pair
map.put("cat",""furry\n white"); // \n So that you will get a new line character in text area
map.put("dog","greyhound \n beast");
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(rows,columns));//In your case (map.size,2)
frame.setSize(300, 300);
//Now You need to Iterate through the Map.
for(Map.Entry<String,String>entry:map.entrySet()){
JTextArea left=JTextArea(entry.getKey());
JTextArea right=JTextArea(entry.getValue());
frame.add(left); //Adding each key to the Frame on left side
frame.add(right); //This is the value you want in side of key
}
frame.setVisible(true);
你会得到类似这样的东西,但格式是可编辑的
I have two ArrayLists called keys and values.
keys = {"cat", "dog", "mouse"} values = {"furry;white", "greyhound;beast", "little;rat"}
如您所见,您正在单独维护键和值列表,这是一种不好的做法,您可以使用 Map(只需阅读此 link,它会有所帮助)存储key
和它对应的 value
在一条记录中,
现在提一点建议
如果你想对一个key
使用multiple values
,我建议你使用MultiMap,它针对一个键维护多个值,这是非常有用的库。