java - 使用 JOptionPane 显示容纳的数组

标签 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);

我想将其显示为单独的列。任何帮助都会很棒。

最佳答案

我找到了这种方法。它非常简单且易于实现。只需使用 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);

关于java - 使用 JOptionPane 显示容纳的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26227804/

相关文章:

java - 从对象映射到动态字符串

java - 将行/单元添加到 JTable

c - 当数组大小为一百万时程序崩溃

c++ - vector 数组 C++ - 添加元素时的奇怪行为

arrays - ruby 优雅地将一组项目包装到一个对象中

java - JLabel 覆盖 JMenuBar

java - 我该如何解决这个错误

java - Simple Counter应用在启动时崩溃,没有任何错误

java - 返回较高值中位数的中值方法

java - io 相关 actor 的 Akka 调度程序配置