JOptionPane Multidimensional Array output

断了今生、忘了曾经 提交于 2019-12-02 17:45:44

问题


Ok say I have Multidimensional array that I want to display in JOptionPane.showMessageDialog. I know that when using System.out.println, you use a for loop. However the array size is determined by the user input, therefore I have to use a incrementor.

For example: userinput[k] next to usernumber[k] then the next row would be userinput[k+1] next to usernumber[k+1]

The trouble I am having is that by using my loop, it does each set one at a time in separate windows and not all together in a table in one window.

for (int k = 0; k < userinput.length; k++){
    JOptionPane.showMessageDialog(null, userinput[k]);
}

for (int k = 0; k < 2; k++){
    JOptionPane.showMessageDialog(null, usernumber[k]);
}

回答1:


Build your output into a single String.

You can use html tags to provide additional formatting

StringBuilder sb = new StringBuilder(64);
sb.append("<html><table>");
for (int ui = 0; ui = < userinput.length; ui++) {
    sb.append("<tr><td>");
    sb.append(userinput[ui]);
    sb.append("</td>");
    for (int k = 0; k < 2; k++){
        sb.append("<td>");
        sb.append(usernumber[k]);
        sb.append("</td>");
    }
    sb.append("</tr>");
}
sb.append("</table></html>");
JOptionPane.showMessageDialog(null, sb.toString());



回答2:


Use a StringBuilder and concatenate each element of the array into a display message,



来源:https://stackoverflow.com/questions/15824033/joptionpane-multidimensional-array-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!