简体   繁体   English

如何在单个JOptionPane方法中输出数组的所有值?

[英]How can I output all values of an array in a single JOptionPane method?

I want to output all the values in a variable in the same sentence in a showMessageDialog method, I know I could use a JList to put them all in one screen, but I'd rather have something like: 3,4,2,62,12,41,5 我想在showMessageDialog方法的同一句子中输出变量中的所有值,我知道我可以使用JList将它们全部放在一个屏幕中,但是我希望有类似的内容:3,4,2,62 ,12,41,5

I could do something like 我可以做类似的事情

 for(int x = 0;x < array.length;x++){
JOptionPane.showMessageDialog(null,array[x] + ",");}

But that would take more than one screen and it's not what I want. 但这将需要多个屏幕,这不是我想要的。

Also, perhaps it could be done with a JLabel instead, I'll suit myself with that if it's easier. 另外,也许可以用JLabel代替,如果更简单,我就适合自己。

As shown here , use StringBuilder to construct a line-oriented representation of your array and display it in a JScrollPane in a JOptionPane . 如所示在这里 ,使用StringBuilder来构建你阵列的面向行的表示,并在显示它JScrollPane一个JOptionPane The scroll pane's preferred size can be arbitrary. 滚动窗格的首选大小可以是任意的。

图片

Do the concatenation first: 首先进行串联:

String s = Arrays.toString(array);
s = s.substring(1,s.length-1);
JOptionPane.showMessageDialog(null,s);

The problem with this is, if the array is sufficiently large, then it will not fit on the screen anyway. 这样的问题是,如果阵列足够大,则无论如何它将不适合屏幕。 Presenting it in a messagebox may not be usefull to the user. 在消息框中显示它可能对用户没有用。

If you need to show this in a manageable way for arbitrary large arrays, then I would rather write a small dialog class, which is not much work, and then use a scrollable textarea instead. 如果您需要以可管理的方式显示任意大数组,那么我宁愿编写一个小的对话框类,这工作不多,然后改用可滚动文本区域。

This way you can prepare it in any way you want to. 这样,您可以按照自己想要的任何方式进行准备。 However, from your short sample it's not entirely clear if you always have a small number of items, in which case creating the string should be sufficient. 但是,从您的简短样本中并不能完全确定您是否总是有少量项目,在这种情况下,创建字符串就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM