使用JOptionPane显示容纳的数组

时间:2014-10-07 02:43:15

标签: java arrays swing joptionpane

我有这个代码,它运行得很好。问题是,当我尝试使用showMessageDialog显示它时,它以凌乱的方式显示所有内容。有关如何正确显示它的任何建议?感谢。

String tablaColeccion;
tablaColeccion ="";
for (int i = 0; i < informeMatrices.getVector().length; i++){
    if (informeMatrices.getVector()[i] != null){
        tablaColeccion += informeMatrices.getVector()[i] + " ";
    }
}
tablaColeccion += "\n";

for (int i = 0; i < informeMatrices.getMatriz().length; i++) {
    for(int u = 0; u < informeMatrices.getMatriz().length; u++){
        if (informeMatrices.getMatriz()[i][u] != null){
            tablaColeccion += informeMatrices.getMatriz()[i][u] + " ";
        }
    }
    tablaColeccion += "\n"; 
}
tablaColeccion += "\n\n";

JOptionPane.showMessageDialog(null,tablaColeccion,"Coleccion Completa", JOptionPane.INFORMATION_MESSAGE);

我想将其显示为分隔列。任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

我找到了这种方法。它非常简单易于实现。只需使用HTML即可将所有内容容纳到列和行中。

StringBuilder sb = new StringBuilder( 128);

        sb.append("<html><table><tr>");

        for (int i = 0; i < informeMatrices.getVector().length; i++)
        {
            if (informeMatrices.getVector()[i] != null)
            {

                sb.append("<td>" + informeMatrices.getVector()[i] + "</td>");
            }
        }

        for (int i = 0; i < informeMatrices.getMatriz().length; i++) 
        {
            sb.append("<tr>");

            for(int u = 0; u < informeMatrices.getMatriz().length; u++)
            {
                if (informeMatrices.getMatriz()[i][u] != null)
                {                       
                    sb.append("<td>").append(informeMatrices.getMatriz()[i][u]).append("</td>");
                }

            }

            sb.append("</tr>");

        }

        sb.append("</table></html>");

        JOptionPane.showMessageDialog(null,sb.toString(),"Coleccion Completa",
                JOptionPane.INFORMATION_MESSAGE);