gpt4 book ai didi

java - 我如何将 Java 中的二维数组打印到 Swing/JOptionPane 中的窗口

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:33 25 4
gpt4 key购买 nike

我真的很难过。我无法在任何地方找到如何在 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));

}

}

如果有人知道如何执行此操作,将非常感谢您的帮助。

附言StarWars 类存在,因为它是家庭作业;)

最佳答案

有许多可能的方法,这里是一个:您可以使用 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("&nbsp&nbsp");
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()));

关于java - 我如何将 Java 中的二维数组打印到 Swing/JOptionPane 中的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41224557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com