【问题标题】:Printing 2d array in JOptionPane在 JOptionPane 中打印二维数组
【发布时间】:2017-12-05 16:33:38
【问题描述】:

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

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

row 和 col 也是随机的。

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

【问题讨论】:

    标签: java arrays swing function joptionpane


    【解决方案1】:

    如果您希望将您的号码放置在表格中,您需要创建一个带有GridLayoutJPanel 以及一些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 列”,因此这适用于 1D 或 2D 数组,只是改变了从每个数组获取数据的方式。


    编辑:

    但我忘了说这是学校作业。我们不允许弄乱 JOptionPane。它应该保持这样: JOptionPane.showMessageDialog(null, 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);
        }
    }
    

    【讨论】:

    • 感谢您的回答。但我忘了说这是学校作业。我们不允许弄乱 JOptionPane。它应该保持这样: JOptionPane.showMessageDialog(null, String);
    • 我很乐意提供帮助 :)
    • 要打印的矩阵是由 1-127 的随机数和 1-10 的随机维度填充的其他 2 个矩阵的乘积
    • 那么......最大的数字是多少?
    • 32258 最高
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    相关资源
    最近更新 更多