簡體   English   中英

Java-JOptionPane中的二維數組如何一次顯示所有內容

[英]Java - 2 dimensional array in JOptionPane how to show it all at once

我有一個二維數組,我想通過JOptionPane顯示。 到目前為止,它一次只顯示1行。 但我希望它一次顯示所有8行。

一旦我運行代碼,它還會在JOptionPane中顯示方括號和逗號。 有什么辦法擺脫那些括號和逗號?

到目前為止,這是我的代碼,我剛剛開始學習Java。

package indzendopgave2;
import javax.swing.JOptionPane;

import java.util.Arrays;

public class Inzend2 {

public static void main(String[] args) {
    //Creating array
    int[][] blastTable = new int[][]{
            {32,31,30,29,28,27,26,25},
            {24,23,22,21,20,19,18,17},
            {16,15,14,13,12,11,10,9},
            {8,7,6,5,4,3,2,1},
            {0,-1,-2,-3,-4,-5,-6,-7},
            {-8,-9,-10,-11,-12,-13,-14,-15},
            {-16,-17,-18,-19,-20,-21,-22,-23},
            {-24,-25,-26,-27,-28,-29,-30,-31}
    };

    printArray(blastTable);
}



//Method to print two dimensional array in a JOptionPane
public static void printArray(int[][] num1){

    for(int x=0; x<num1.length; x++){
        String output = Arrays.toString(num1[x]);
            JOptionPane.showMessageDialog(null, output, "Uitvoer",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }
}

如果要一次顯示整個數組,則根本不需要使用循環。

   String output = Arrays.deepToString(num1);
   JOptionPane.showMessageDialog(null, output, "Uitvoer",
                    JOptionPane.INFORMATION_MESSAGE);

如果你想刪除的,[]您必須將數組解析到一個字符串,只要你喜歡格式化。

這也可以

  public static void printArray(int[][] num1) {
    String output = "";
    for (int x = 0; x < num1.length; x++) {
        output += Arrays.toString(num1[x]) + "\n";
    }
    JOptionPane.showMessageDialog(null, output, "Uitvoer",
            JOptionPane.INFORMATION_MESSAGE);
}

使用最佳工具完成工作,並在Swing中顯示表格數據,這意味着創建JTable。 我將使用您的數據創建一個DefaultTableModel對象,將模型放入JTable中,然后在JOptionPane中顯示JTable。 解決了。

在此處輸入圖片說明

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.util.Arrays;

public class Inzend2 {

   public static void main(String[] args) {
      // Creating array
      int[][] blastTable = new int[][] { { 32, 31, 30, 29, 28, 27, 26, 25 },
            { 24, 23, 22, 21, 20, 19, 18, 17 },
            { 16, 15, 14, 13, 12, 11, 10, 9 }, { 8, 7, 6, 5, 4, 3, 2, 1 },
            { 0, -1, -2, -3, -4, -5, -6, -7 },
            { -8, -9, -10, -11, -12, -13, -14, -15 },
            { -16, -17, -18, -19, -20, -21, -22, -23 },
            { -24, -25, -26, -27, -28, -29, -30, -31 } };

      printArray(blastTable);
   }

   // Method to print two dimensional array in a JOptionPane
   public static void printArray(int[][] num1) {
      String[] columnNames = { "A", "B", "C", "D", "E", "F", "G", "H" };

      // table models expect reference type data, and so
      // int array must be changed to an Integer array
      Integer[][] data = new Integer[num1.length][num1[0].length];
      for (int i = 0; i < data.length; i++) {
         for (int j = 0; j < data[i].length; j++) {
            data[i][j] = num1[i][j];
         }
      }
      DefaultTableModel model = new DefaultTableModel(data, columnNames);
      JTable table = new JTable(model);
      JScrollPane scrollPane = new JScrollPane(table);

      JOptionPane.showMessageDialog(null, scrollPane, "Uitvoer",
            JOptionPane.INFORMATION_MESSAGE);
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM