简体   繁体   English

Java-JOptionPane中的二维数组如何一次显示所有内容

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

I have a two dimensional array, which i want to show via a JOptionPane. 我有一个二维数组,我想通过JOptionPane显示。 So far it is showing me 1 row at a time. 到目前为止,它一次只显示1行。 But i would like it to show all 8 rows at once. 但我希望它一次显示所有8行。

It is also showing brackets and comma's in the JOptionPane once i run the code. 一旦我运行代码,它还会在JOptionPane中显示方括号和逗号。 Is there some way to get rid of those brackets and comma's? 有什么办法摆脱那些括号和逗号?

This is my code so far, im just started learning Java. 到目前为止,这是我的代码,我刚刚开始学习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);
        }
    }
}

If you want to show the whole array all at once, you need not use a loop at all. 如果要一次显示整个数组,则根本不需要使用循环。

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

If you want to remove the , and [] , you have to parse the array to a string and format it as you like. 如果你想删除的,[]您必须将数组解析到一个字符串,只要你喜欢格式化。

This will also do 这也可以

  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);
}

Use the best tool for the job, and for displaying tabular data in Swing, this means creating a JTable. 使用最佳工具完成工作,并在Swing中显示表格数据,这意味着创建JTable。 I'd create a DefaultTableModel object with your data, put the model into a JTable, and then display the JTable in your JOptionPane. 我将使用您的数据创建一个DefaultTableModel对象,将模型放入JTable中,然后在JOptionPane中显示JTable。 Solved. 解决了。

在此处输入图片说明

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