在JOptionPane中打印2d数组

时间:2017-12-05 16:33:38

标签: java arrays swing function joptionpane

我尝试在JOptionPane中打印随机的1d和2d数组。 但它是这样的:

enter image description here

有没有办法将它格式化为这样排列:

enter image description here

行和col也是随机的。

另一件事,有没有办法只使用一个函数来打印1d和2d数组?如果没有,2d功能可以使用第一个吗? 这是为学校作业。我们不允许搞乱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;
  }

1 个答案:

答案 0 :(得分:1)

如果您希望将您的号码放置在表格中,则需要使用GridLayout创建一个JPanel,其中包含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);
    }
}

这会产生这样的结果:

enter image description here enter image description here

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

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

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

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

编辑:

  

但我忘了提到这是为了学校的功课。我们不允许搞乱JOptionPane。它应该保持这样:JOptionPane.showMessageDialog(null,String);

您可以在String中使用HTML标签将其转换为表格,然后将每个数字放在其单元格内,使用一些cellspacing来提供更多或更少的空间(使用它) :

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

enter image description here