我花了一整天在網絡上與本網站上尋找的答案,我的問題,希望你們能幫忙。首先,我想,當我選擇「報告」 JButton
顯示一個ArrayList
到JTextArea
的內容。數組列表位於與文本區域分開的另一個類中。顯示一個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;
}
我希望我已經澄清了我的問題,沒有任何人混淆。任何有識之士將不勝感激。名單上
你重寫了TransactionObject的toString()方法嗎? –
您不能使用系統實用程序來繪製數據,使用繪圖表面並在其中繪製對象。 –