試圖讓我的按鈕附接至一個ActionListener到填充我JTextArea中的項目的用戶輸入的號碼的一個新的隨機生成的陣列,從另一個類/方法產生的。我可以部分運作,但不完全。爪哇:顯示陣列已創建從方法在另一類
我能得到這個具有按鈕創建一個全新的GUI窗口的工作,但是這不是我想要的,我只是想在JTextArea中與新生成的陣列追加。按原樣,JTextArea顯示數組的類信息和十六進制代碼,而不是別的。
功能:該用戶輸入的整數成箱,它告訴方法多少項目來填充所述陣列,點擊一個按鈕,並且JTextArea中被清除之後出現一個隨機生成的陣列。
我猜(希望),這是一個相當簡單的問題。我已經把這兩個.java文件中的所有適用的(我認爲)代碼放在一起。
Automobile.java:
//toString method override for formatting
@Override
public String toString() {
return String.format
(" %d\t\t%s\t\t%s\t\t%d\t%1.1fL", invID, make, model, doors, engineSize);
}// end method toString
//This method randomizes the array
public static Automobile[] makeRandom(int m) {
Automobile[] auto = new Automobile[m];
for (int i = 0; i < auto.length; i++) {
auto[i] = new Automobile();
auto[i].make = carMakes.get(randomGen.nextInt(carMakes.size()));
auto[i].model = carModels.get(randomGen.nextInt(carModels.size()));
auto[i].doors = randomGen.nextInt(6);
if(auto[i].doors == 0 || auto[i].doors == 1)
auto[i].doors = 2;
auto[i].engineSize = randomGen.nextDouble() * 6.0;
if(auto[i].engineSize <= 1.49)
auto[i].engineSize = 1.5;
} // end for method to instantiate
return auto;
} // end method makeRandom
//Main method for creating the GUI with default number of array entries
public static void main(String args[]) {
Automobile[] x = makeRandom(20);
GUI frame = new GUI(x);
frame.setVisible(true);
}
GUI.java
//Creates the GUI
public GUI(Automobile[] ia) {
this.autoArray = ia;
initComponents();
for (Automobile m : autoArray) {
mainTextArea.append(m.toString()+"\n");
}
Arrays.sort(autoArray);
mainTextArea.append("\n\n---- Sorted By Make -----\n");
for (Automobile m : autoArray) {
mainTextArea.append(m.toString() + "\n");
}
mainTextArea.append("\n\n---- Sorted By Model -----\n");
Automobile.sortBy = Automobile.SORTBY.MODEL;
Arrays.sort(autoArray);
for (Automobile m : autoArray) {
mainTextArea.append(m.toString() + "\n");
}
mainTextArea.append("\n\n---- Sorted By Doors -----\n");
Automobile.sortBy = Automobile.SORTBY.DOORS;
Arrays.sort(autoArray);
for (Automobile m : autoArray) {
mainTextArea.append(m.toString() + "\n");
}
mainTextArea.append("\n\n---- Sorted By Engine Size -----\n");
Automobile.sortBy = Automobile.SORTBY.ENGINESIZE;
Arrays.sort(autoArray);
for (Automobile m : autoArray) {
mainTextArea.append(m.toString() + "\n");
}
}//End GUI constructor
//Method for the button click action
private void randomizeButtonActionPerformed(java.awt.event.ActionEvent evt) {
int number = Integer.parseInt(numCarsTextField.getText());
mainTextArea.setText(null);
Automobile[] x = makeRandom(number);
mainTextArea.append(x.toString());
}
會很感激一些指導。
謝謝!
添加一些我正在嘗試的代碼,由@Hovercraft建議Full Of Eels,這是新方法,但我甚至不知道如何處理它,我真的很感謝一些更深入的幫助在這裏:
public void appendAutos(Automobile[] cars){
this.autoArray = cars;
initComponents();
for (Automobile m : autoArray) {
mainTextArea.append(m.toString()+"\n");
}
Arrays.sort(autoArray);
mainTextArea.append("\n\n---- Sorted By Make -----\n");
for (Automobile m : autoArray) {
mainTextArea.append(m.toString() + "\n");
}
mainTextArea.append("\n\n---- Sorted By Model -----\n");
Automobile.sortBy = Automobile.SORTBY.MODEL;
Arrays.sort(autoArray);
for (Automobile m : autoArray) {
mainTextArea.append(m.toString() + "\n");
}
mainTextArea.append("\n\n---- Sorted By Doors -----\n");
Automobile.sortBy = Automobile.SORTBY.DOORS;
Arrays.sort(autoArray);
for (Automobile m : autoArray) {
mainTextArea.append(m.toString() + "\n");
}
mainTextArea.append("\n\n---- Sorted By Engine Size -----\n");
Automobile.sortBy = Automobile.SORTBY.ENGINESIZE;
Arrays.sort(autoArray);
for (Automobile m : autoArray) {
mainTextArea.append(m.toString() + "\n");
}
}
private void randomizeButtonActionPerformed(java.awt.event.ActionEvent evt) {
int number = Integer.parseInt(numCarsTextField.getText());
mainTextArea.setText(null);
appendAutos(Automobile[]);
我不知道什麼樣的返回類型應該是或如何將用戶輸入的數字傳遞到方法,因爲它表明,該方法接受一個數組類型。
是否有意義調用'的initComponents()'從'appendAutos(...)'方法中?在複製代碼之前,請考慮每行代碼的作用。 –