如何在多行中打印 JOptionPane 中的数组

How to print an array in a JOptionPane in multiple lines

首先,我不是母语人士,所以请原谅我的错误^^

我正在编写一个程序,将所有素数获取到特定整数,并希望在最后在 JOptionPane 中打印所有素数。

实际上,我确实在一个单独的函数中计算所有素数,return它们都在一个整数数组中,将这个数组安全地放入一个字符串中,然后在 JOptionPane 中打印该字符串。

但不幸的是,我只有一个!!打印所有整数的超长行,横跨两个显示器。因此,当我打印例如所有不超过 10000 的素数时,我在一行中得到了所有素数。

我想要,例如,一行中只有 100 个素数,然后在下一行打印其余素数。但是我怎样才能做到这一点?

    check_button1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

                int x = Integer.parseInt(InputField1.getText());
                int prime_numbers[];


                String all_prime_String = Arrays.toString(number_theory.primes(x));
                System.out.println(Arrays.toString(number_theory.primes(x)));


                JOptionPane.showMessageDialog(Math_Collection_Gui.this, all_prime_String, "Ergebnis",  JOptionPane.INFORMATION_MESSAGE);

        }
    });

number_theory.primes(x) 函数 return 是 x 之前所有素数的数组,然后我将这个数组保存在一个字符串中。

就像我说的,我的问题是,数组的所有值都打印在一行中。那么是否有可能,为了固定盒子的长度或其他东西,程序被迫跳到下一行?或者你有什么其他的技巧给我吗?

最简单的选择可能是只读文本区域:

JTextArea textArea = new JTextArea(all_prime_String, 8, 20);
textArea.setLineWrap(true);
textArea.setEditable(false);

JOptionPane.showMessageDialog(Math_Collection_Gui.this, new JScrollPane(textArea), "Ergebnis",  JOptionPane.INFORMATION_MESSAGE);

当然还有更易读的选择,例如将每个值放在 JList 行中,如果您愿意的话。

如果您想在文本区域下方放置一些文本,请将对象数组作为 JOptionPane 的消息传递:

Object[] message = {
    new JScrollPane(textArea),
    "Additional text here",
};
JOptionPane.showMessageDialog(Math_Collection_Gui.this, message, "Ergebnis",  JOptionPane.INFORMATION_MESSAGE);