2013-07-27 34 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。