如何使用内部JOptionPane迭代数组并显示自定义消息?

时间:2014-12-26 02:02:55

标签: java swing joptionpane

我正在完成一个学校项目(我的第一个Java课程),我被告知仅使用JOptionPane 我需要使用JOptionPane而不是System.out.println执行 之类的操作:

int[] den = {1000, 500, 100 ,50, 20, 10 ,5, 2, 1};
System.out.println("\nDENOMINATIONS: \n");

for (int i = 0; i < 9; i++) {
    count = amount / den[i]; // counting number of den[i] notes
    if (count != 0) { //printing that denomination if the count is not zero
        System.out.println(den[i] + "\tx\t" + count + "\t= " + den[i] * count);
    }
    totalNotes = totalNotes + count; //finding the total number of notes
    amount = amount%den[i]; //finding the remaining amount 
}
System.out.println("--------------------------------");
System.out.println("TOTAL\t\t\t= " + copy); 
System.out.println("--------------------------------");
System.out.println("Total Number of Notes\t= " + totalNotes); 

打印出如此美丽的东西:

DENOMINATIONS:

1000     x     14     =   14000
500      x     1      =   500
100      x     3      =   300
50       x     1      =   50
5        x     1      =   5
1        x     1      =   1
————————————–
TOTAL                 =   14856
————————————–
Total Number of Notes = 21

1 个答案:

答案 0 :(得分:3)

阅读JOptionPane API。您可以将任何组件添加到选项窗格。 &#34;消息&#34;参数可以是显示文本的简单字符串,也可以是Swing组件,也可以是包含字符串和组件的数组,在这种情况下,多个组件将垂直添加到选项窗格中。

因此,您可以将文本添加到JTextArea。然后将文本区域添加到选项窗格中。

您还需要将文本区域的字体更改为等宽字体,以便正确对齐文本。