在 JOptionPane 中打印二维数组

Printing 2d array in JOptionPane

我尝试在 JOptionPane 中打印随机一维和二维数组。 但结果是这样的:

有没有办法把它格式化成这样排列:

行和列也是随机的。

另一件事,有没有办法只使用一个函数来打印一维和二维数组?如果不能,二维函数可以使用第一个吗? 这是学校作业。我们不允许乱用 JOptionPane。它应该保持这样:JOptionPane.showMessageDialog(null, String);

 public static String printMatrix(int[] m) {
    String result = "";
    for (int i = 0; i < m.length; i++) {
      result += m[i] + "                ";
    }
    return result;
  }


  public static String printMatrix2D(int[][] m) {
    String result = "";
    for (int i = 0; i < m.length; i++) {
      for (int j = 0; j < m[i].length; j++) {
        result += m[i][j] + "                ";
      }
      result += "\n";
    }
    return result;
  }

如果你想让你的数字像在 table 中一样放置,你需要创建一个 JPanel 和一个 GridLayout 以及一些 hgapvgap。这适用于 4 位和 5 位数字或任意数量的数字。

例如:

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JOptionPanePrintingInColumns {

    private JPanel pane;
    private String[] oneDArray = new String[] {"1000", "10000", "99999", "23000", "100000"};
    private String[][] twoDArray = new String[][] {
        {"1000", "10000", "99999", "23000", "100000"},
        {"1000", "10000", "99999", "23000", "100000"}, 
        {"1000", "10000", "99999", "23000", "100000"},
        {"1000", "10000", "99999", "23000", "100000"}, 
        {"1000", "10000", "99999", "23000", "100000"}
    };
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new JOptionPanePrintingInColumns()::createAndShowGui);
    }
    
    private void createAndShowGui() {
        //For the 1D Array
        pane = new JPanel();
        pane.setLayout(new GridLayout(0, 5, 50, 10));
        for (int i = 0; i < oneDArray.length; i++) {
            JLabel label = new JLabel(oneDArray[i]);
            label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            pane.add(label);
        }
        JOptionPane.showMessageDialog(new JFrame(), pane);
        
        //For the 2D array
        pane = new JPanel();
        pane.setLayout(new GridLayout(0, 5, 50, 10));
        for (int i = 0; i < twoDArray.length; i++) {
            for (int j = 0; j < twoDArray[i].length; j++) {
                JLabel label = new JLabel(twoDArray[i][j]);
                label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                pane.add(label);
            }
        }
        JOptionPane.showMessageDialog(new JFrame(), pane);
    }
}

这会产生这样的东西:

并且因为我们可以看到边框,所以我们可以注意到每个标签都与最大的标签(即每一行中的最后一个)大小相同,这就是为什么它适用于较大或较短的数字。

您可能已经注意到我使用了这一行:

pane.setLayout(new GridLayout(0, 5, 50, 10));

但是 0 是什么意思?这意味着 “根据需要创建尽可能多的行,每行 5 列”,因此这适用于一维或二维数组,只需更改从每个数组获取数据的方式即可。


编辑:

but i forgot to mention this is for school homework. we are not allowed to mess with JOptionPane. it's supposed to stay like this: JOptionPane.showMessageDialog(null, String);

您可以在字符串中使用 HTML 标签将其转换为 table,然后将每个数字放在它的单元格中,使用一些 cellspacing 来提供或多或少space(一起玩):

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JOptionPanePrintingInColumns {

    private JPanel pane;
    private String[] oneDArray = new String[] {"1000", "10000", "99999", "23000", "100000"};
    private String[][] twoDArray = new String[][] {
        {"1000", "10000", "99999", "23000", "100000"},
        {"100", "180000", "9999", "3000", "1090000"}, 
        {"111000", "1220000", "9955999", "230100", "1000200"},
        {"10010", "1005400", "9999954", "9", "123"}, 
        {"100430", "1000054", "999", "23123000", "123456789"}
    };
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new JOptionPanePrintingInColumns()::createAndShowGui);
    }
    
    private void createAndShowGui() {
        String output;
        StringBuilder sb = new StringBuilder();
        sb.append("<html><table><tr>");
        for (int i = 0; i < oneDArray.length; i++) {
            sb.append("<td>").append(oneDArray[i]).append("</td>");
        }
        sb.append("</tr></table></html>");
        output = sb.toString();
        JOptionPane.showMessageDialog(new JFrame(), output);
        
        sb = new StringBuilder();
        sb.append("<html><table cellspacing=10>");
        for (int i = 0; i < twoDArray.length; i++) {
            sb.append("<tr>");
            for (int j = 0; j < twoDArray.length; j++) {
                sb.append("<td>").append(twoDArray[i][j]).append("</td>");
            }
        }
        sb.append("</tr></table></html>");
        output = sb.toString();
        JOptionPane.showMessageDialog(new JFrame(), output);
    }
}