我无法按照我想要的方式显示和排列数组。不是显示完整的字符串列表,而是每个对话框一次显示一个String。一旦我在对话框中确实没问题,阵列中的下一个项目会弹出一个新项目,直到它完成所有这些项目。我想让所有这些都弹出一个对话框。任何帮助表示赞赏。
b3.addActionListener(new ActionListener() {
/**
* Displays the arraylist.
*/
public void actionPerformed(ActionEvent e) {
ImageIcon icon = new ImageIcon(Window.class.getResource("/car.png"));
for(int i=0; i < cars.size(); i++) {
JTextArea textArea = new JTextArea(cars.get(i) + '\n');
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
scrollPane.setPreferredSize(new Dimension( 150, 200 ));
JOptionPane.showMessageDialog(null, scrollPane);
答案 0 :(得分:0)
您可以在for循环中创建文本区域和其他小部件。您希望在循环外创建这些,然后将文本区域的内容设置为ArrayList的字符串表示。
试试这个:
ImageIcon icon = new ImageIcon(Window.class.getResource("/car.png"));
StringBuilder sb = new StringBuilder(); // this will be the contents of your list
for(int i=0; i < cars.size(); i++) {
sb.append(cars.get(i) + "\n"); // add each element to the string
}
// create the text area widgets
JTextArea textArea = new JTextArea(sb.toString()); // add the string you built
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
scrollPane.setPreferredSize(new Dimension( 150, 200 ));
JOptionPane.showMessageDialog(null, scrollPane);