我有一个使用 println 打印格式良好的二维数组的 java 程序。在eclipse中,这个数组当然会打印到控制台。但是,我在同一个项目中制作了一个单独的 SWT 程序,我希望将此数组打印到文本小部件而不是 Eclipse 控制台。我该怎么做呢?
public static void main(String[] args) throws IOException {
try {
ReadFile file = new ReadFile(path);
String[][] pf = file.aryData();
int tmp = 1;
int yn=1;
while(yn==1){
Scanner in = new Scanner(System.in);
System.out.print("To View-0 , Add-1 ");
yn = in.nextInt();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(path, true)));
out.println("");
if (yn == 1) {
String[][] newpf = new String[pf.length + 1][c];
String newData1;
System.arraycopy(pf, 0, newpf, 0, pf.length);
for (int i = 0; i < c; i++) {
System.out.print("Insert " + pf[0][i]+": ");
if (i==0){
in.nextLine();
newData1 = in.nextLine();
out.print(newData1);
}
else {
newData1 = in.nextLine();
out.print(", "+newData1);
}
newpf[pf.length][i] = newData1;
}
System.out.print("");
pf=newpf;
out.close();
}
}
for (int i = 0; i < pf.length; i++) {
for (int k = 0; k < c; k++) {
int tmp2 = pf[i][k].length();
if (tmp2 > tmp) {
tmp = tmp2 + 5;
}
}
}
for (int i = 0; i < pf.length; i++) {
for (int j = 0; j < c; j++) {
String fmStr = "%-" + tmp + "s";
System.out.printf(fmStr, pf[i][j]);
}
System.out.println("");
}
}
}