將隨機生成的整數數組逐個顯示在 JTextArea 界面上,嘗試 setText 和 append 均無效果。
利用 Arrays.toString(array) 將數組轉為字符串,直接作為 JTextArea 構造參數或 setText 參數。
int[] numbers = new int[20];
// 填充隨機數...
String text = Arrays.toString(numbers); // "[1, 5, 3, ...]"
JTextArea textArea = new JTextArea(text);
textArea.setEditable(false);
// 加入滾動面板並顯示...
SwingUtilities.invokeLater(() -> { ... }) 包裹。這樣即可一次性將整個數組內容完整顯示在文本區域中。