如果我使用的是JOptionPane消息对话框,我怎样才能在消息部分显示整个数组,例如这个小片段?或者甚至可能吗?

 public void showTheMessage()

{
 JOptionPane.showMessageDialog(null,"These are are all the colors to
          choosfrom,\n"+ arrayOfcolors[the whole array], "Color box");
 }
有帮助吗?

解决方案

最简单的方法是将数组的所有元素连接成一个大字符串。

String colors = "";
for(int i = 0; i < arrayOfColors.length; i++)
    colors += arrayOfColors[i] + " ";

其他提示

showOptionDialog 方法允许用户从一组选项中选择一个元素,我认为这是什么你正在寻找。

如果是一个Color对象数组

   String colors="";
   for (Color c: arrayOfColors) 
       colors+= c.toString() + " ";

否则,如果是一个String对象数组

   String colors="";
   for (String s: arrayOfColors) 
       colors+= s + " ";

请注意,使用StringBuilder要快得多,但我猜这只是一个小阵列。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top