2013-07-27 40 views
0

我花了一整天在網絡上與本網站上尋找的答案,我的問題,希望你們能幫忙。首先,我想,當我選擇「報告」 JButton顯示一個ArrayListJTextArea的內容。數組列表位於與文本區域分開的另一個類中。顯示一個ArrayList <Object>在JTextArea中其他類

The method append(String) in the type JTextArea is not applicable 
    for the arguments (ArrayList.Account.TransactionObject>) 

我可以顯示數組列表只需在控制檯窗口罰款,但我:我的問題與事實數組列表是對象的數組,所以,當我嘗試以顯示它我得到的錯誤莖當它在文本區域中顯示時難倒了。我假設必須有某種問題將對象轉換爲字符串,因爲我無法將其轉換爲字符串或使用數組列表調用toString方法。這裏是我的代碼的相關部分.....

這是AccountUI類哪裏創建的JTextArea的部分:

private JPanel get_ReportPane() 
{ 
    JPanel JP_reportPane = new JPanel(new BorderLayout()); 
    Border blackline = BorderFactory.createLineBorder(Color.BLACK); 
    TitledBorder title = BorderFactory.createTitledBorder(blackline, "Transaction Report"); 
    title.setTitleJustification(TitledBorder.CENTER); 
    JP_reportPane.setBorder(title); 

    /* Create 'labels' grid and JLabels */ 
    JPanel report_labels = new JPanel(new GridLayout(2, 1, 5, 5)); 
    report_labels.add(new JLabel("Current Account Balance: ", SwingConstants.RIGHT)); 
    report_labels.add(new JLabel("Account Creation Date: ", SwingConstants.RIGHT)); 
    JP_reportPane.add(report_labels, BorderLayout.WEST); 

    /* Create 'data' grid and text fields */ 
    JPanel JP_data = new JPanel(new GridLayout(2, 1, 5, 5)); 
    JP_data.add(TF_balance2 = new JTextField(10)); 
    TF_balance2.setBackground(Color.WHITE); 
    TF_balance2.setEditable(false); 
    JP_data.add(TF_created = new JTextField(10)); 
    TF_created.setBackground(Color.WHITE); 
    TF_created.setEditable(false); 
    JP_reportPane.add(JP_data, BorderLayout.CENTER); 

    /* Create 'buttons' grid and buttons */ 
    JPanel JP_buttons = new JPanel(new GridLayout(2, 1, 5, 5)); 
    JButton JB_report = new JButton("Report"); 
    JB_report.setBackground(Color.GRAY); 
    JB_report.setMargin(new Insets(3, 3, 3, 3)); 
    JB_report.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      reportAccount(); 
     } 
    }); 


    JP_buttons.add(JB_report); 
    JButton JB_close = new JButton("Close"); 
    JB_close.setBackground(Color.GRAY); 
    JB_close.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent arg0) 
     { 
      System.exit(0); 
     }   
    }); 

    JP_buttons.add(JB_close); 
    JP_reportPane.add(JP_buttons, BorderLayout.EAST); 

    /* Create text area and scroll pane */ 
    reportArea.setBorder(blackline); 
    reportArea.setForeground(Color.BLUE); 
    reportArea.setLineWrap(true); 
    reportArea.setWrapStyleWord(true); 
    JScrollPane scrollPane = new JScrollPane(reportArea); 
    reportArea.setEditable(false); 

    JP_reportPane.add(scrollPane, BorderLayout.SOUTH); 

    return JP_reportPane; 
} 

這是上面所示的方法(從JB_reportAction監聽器類稱爲),我嘗試在文本區域(也AccountUI類)來顯示數組列表:

/** 
* Method used to display account transaction history in the text field. 
*/ 
protected void reportAccount() 
{ 
    reportArea.append(A.getTransactions()); 
} 

而且這是在Account類,我能在一個反面來顯示陣列內容的方法唯一的輸出,但一直無法弄清楚如何數組內容傳遞給AccountUI類作爲字符串文本區域中顯示:

public ArrayList<TransactionObject> getTransactions() 
{ 
    for (int i = 0; i < transactionList.size(); i++) 
    { 
     System.out.println(transactionList.get(i)); 
     System.out.println("\n"); 
    } 
    return transactionList; 
} 

我希望我已經澄清了我的問題,沒有任何人混淆。任何有識之士將不勝感激。名單上

+1

你重寫了TransactionObject的toString()方法嗎? –

+0

您不能使用系統實用程序來繪製數據,使用繪圖表面並在其中繪製對象。 –

回答

6

呼叫toString()

reportArea.append(A.getTransactions().toString()); 

或者,如果你想在一個不同的格式顯示在列表中的元素,遍歷元素:

for (TransactionObject transaction : A.getTransactions()) { 
    reportArea.append(transaction.toString()); 
    reportArea.append("\n"); 
} 

循環和類型編程的一個重要部分。如果您不瞭解循環和類型,則不應使用Swing。

此外,請尊重的Java命名約定。變量以小寫字母開頭,不包含下劃線。他們是駱駝的。

+0

是不是有關避免擺動有點不必要的評論。一天需要開始。 +1雖然是一個很好的答案。 – kiheru

+1

@kiheru:Swing很難。它很複雜,它高度依賴於繼承,多態等OO構造。它涉及線程化。試圖在不理解循環的情況下使用Swing會使事情變得比他們應該更加困難。正如你所說,有一天需要開始*。這一天應該*在循環被理解爲基本算法的東西之後*。 –

+0

@AndrewThompson這個問題已經有了一個正確的循環。可以肯定的是使用增強循環寫得更好,但是表明循環的概念可能不是問題。 (相當典型的來自其他語言的人) – kiheru

0

如果你想在ArrayList追加對象的內容JTextArea您可以使用此:

for (Object obj : arrayList) { 
    textArea.append(obj.toString() + ""); 
} 
+0

@VishalD ...我在這裏發佈一個問題之前,我實際上嘗試了一些與此非常相似的東西。但由於ArrayList與另一個與textArea分開的類中,它將ArrayList視爲未定義。如果有一種方法可以將ArrayList導入到其他類中,這是我們尚未學習的東西... –

0

您必須實現並覆蓋的toString TransactionObject。