2016-02-20 49 views
0

試圖讓我的按鈕附接至一個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[]); 

我不知道什麼樣的返回類型應該是或如何將用戶輸入的數字傳遞到方法,因爲它表明,該方法接受一個數組類型。

+0

是否有意義調用'的initComponents()'從'appendAutos(...)'方法中?在複製代碼之前,請考慮每行代碼的作用。 –

回答

1

簡單:給GUI一個setAutos(Automobile[] cars)appendAutos(...)方法,並調用您當前在此方法的構造函數中執行的自動顯示代碼。然後從你的actionPerformed方法中調用該方法。

+0

對不起,您可以詳細說明一下,也許有一些代碼?我想我跟着你,但我正在努力把所有的東西放在一起。謝謝! – bnr32jason

+0

@ bnr32jason:先試一試。你已經得到了你編寫的代碼,所以你應該理解它。 –

+0

但是,如何將用戶輸入的整數傳遞給您建議的方法setAutos?我想,我不能明白的是爲什麼我拉從構造函數的代碼,並把它變成一種新的方法,好像我在做同樣的事情,只是以不同的方式。 – bnr32jason