我真的很难过。我无法在任何地方找到如何在JOptionPane中将此表作为窗口(如ShowMessageDialog)打印,就像在控制台中打印the desired output
一样这是我的代码:
import javax.swing.JOptionPane;
import java.util.*;
public class StarWars {
public static void main(String[] args) {
String [][] gridPlayer = new String [6][6];
Scanner kb = new Scanner (System.in);
String [] rows = {"A", "B", "C", "D", "E", "F", "G", "H"};
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6; j++){
gridPlayer[i][j] = " ~ ";
}
}
System.out.print(" ");
for (int i = 0; i < 6; i++){
System.out.print(" " + rows[i] + " ");
}
System.out.println();
for (int i = 0; i < 6; i++){
System.out.print(" " + i);
for (int j = 0; j < 6; j++){
System.out.print(gridPlayer[i][j]);
}
System.out.println();
}
JOptionPane.showMessageDialog(null, for (int i));
}
}
如果有人知道如何做到这一点,那么你的帮助将会非常受欢迎。
P.S。 StarWars课程出现,因为它是家庭作业;)
答案 0 :(得分:4)
有很多可能的方法,一个是:您可以使用StringBuilder
进行尝试,并使用html标记将所有内容放入JLabel
:
StringBuilder sb = new StringBuilder();
sb.append("<html>");
String [][] gridPlayer = new String [6][6];
Scanner kb = new Scanner (System.in);
String [] rows = {"A", "B", "C", "D", "E", "F", "G", "H"};
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6; j++){
gridPlayer[i][j] = " ~ ";
}
}
sb.append("  ");
for (int i = 0; i < 6; i++){
sb.append(" " + rows[i] + " ");
}
sb.append("<br>");
for (int i = 0; i < 6; i++){
sb.append(" " + i);
for (int j = 0; j < 6; j++){
sb.append(gridPlayer[i][j]);
}
sb.append("<br>");
}
sb.append("</html>");
JOptionPane.showMessageDialog(null, new JLabel(sb.toString()));