如何在带有ArrayList的JOptionPane中显示多行 [英] How do display Multiple Lines in a JOptionPane with ArrayList

查看:109
本文介绍了如何在带有ArrayList的JOptionPane中显示多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在JOptionPane中显示2 Arraylist.但是我在对话框上看到以下内容

I'm trying to display 2 Arraylist in the JOptionPane. But I get the following on the dialog box

1,2,3,4
Swimming, Running, Cycling, Basketball

我想在对话框中显示如下信息:

I would like to show the info in the dialog box like the following:

1 Swimming
2 Running
3 Cycling
4 Basketball

请告知我该怎么做?这是我的代码.

Please advise how do I go about? Here are my codes.

import javax.swing.*;
import java.util.ArrayList;

public class gamelist {
    public static void main(String args[]){

        ArrayList<String> sku = new ArrayList<String> ();
        sku.add("1");
        sku.add("2");
        sku.add("3");
        sku.add("4");


        ArrayList<String> games = new ArrayList<String>();
        games.add("Swimming");
        games.add("Running");
        games.add("Cycling");
        games.add("Basketball");

        for(int i = 0; i<games.size(); i++){

            String everything = sku.toString();
            String everything2 = games.toString();

            JOptionPane.showInputDialog(null, everything +"\n"+ everything2);       
        }

     }

}

推荐答案

您可以很容易地做到这一点. 将这些行放在字符串中作为输出,然后在JOptionPane中将其打印出来.将数据添加到arraylist后,只需执行以下操作.

You can do this very easily. Put those lines in a String as an output and then print that in JOptionPane. After adding your data to arraylist just do the following.

String output = "";
for(int i = 0; i<games.size(); i++){
    String everything = sku.get(i).toString();
    String everything2 = games.get(i).toString();

    output += everything +" "+ everything2 + "\n";       
}
JOptionPane.showMessageDialog(null, output);

这篇关于如何在带有ArrayList的JOptionPane中显示多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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