java 在另一个类的 JTextArea 中显示一个 ArrayList<Object>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17896281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display an ArrayList<Object> in JTextArea of another class
提问by Mister Blue
I have spent all day on the Web and on this site looking for an answer to my problem, and hope you guys can help. First of all, I am trying to display the contents of an ArrayListto a JTextAreawhen I select the 'report' JButton. The array list is in another class separate from the text area. My problem stems from the fact that the array list is an array of objects, so that when I try to display it I get the error:
我花了一整天的时间在网上和这个网站上寻找我的问题的答案,希望你们能帮忙。首先,我想显示的内容ArrayList的JTextArea时候我选择了“报告” JButton。数组列表位于与文本区域分开的另一个类中。我的问题源于这样一个事实,即数组列表是一个对象数组,因此当我尝试显示它时,出现错误:
The method append(String) in the type JTextArea is not applicable
for the arguments (ArrayList.Account.TransactionObject>)
I can display the array list just fine in the console window but am stumped when it comes to displaying it in the text area. I'm under the assumption that there must be some kind of issue converting the Object to a String, because I have been unable to cast it to a String or call a toStringmethod with the array list. Here is the relevant parts of my code.....
我可以在控制台窗口中很好地显示数组列表,但是在文本区域中显示它时我很难过。我假设将 Object 转换为 String 肯定存在某种问题,因为我无法将其转换为 String 或toString使用数组列表调用方法。这是我的代码的相关部分.....
This is the portion in the AccountUIclass where I created the JTextArea:
这是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;
}
This is the method (called from JB_reportActionlistener class shown above) where I try to display the array list in the text area (also in AccountUIclass):
这是JB_reportAction我尝试在文本区域(也在AccountUI类中)中显示数组列表的方法(从上面显示的侦听器类调用):
/**
* Method used to display account transaction history in the text field.
*/
protected void reportAccount()
{
reportArea.append(A.getTransactions());
}
And this is the method in the Account class that I am able to display the Array contents in a console output, but have been unable to figure out how to pass the Array contents to the AccountUIclass as a String to display in the text area:
这是 Account 类中的方法,我能够在控制台输出中显示 Array 内容,但一直无法弄清楚如何将 Array 内容AccountUI作为 String传递给类以在文本区域中显示:
public ArrayList<TransactionObject> getTransactions()
{
for (int i = 0; i < transactionList.size(); i++)
{
System.out.println(transactionList.get(i));
System.out.println("\n");
}
return transactionList;
}
I hope I have clarified my issue without confusing anyone. Any insight would be much appreciated.
我希望我已经澄清了我的问题,而不会混淆任何人。任何见解将不胜感激。
回答by JB Nizet
Call toString()on the list:
调用toString()列表:
reportArea.append(A.getTransactions().toString());
Or, if you want to display the elements of the list in a different format, loop over the elements:
或者,如果您想以不同的格式显示列表的元素,请遍历元素:
for (TransactionObject transaction : A.getTransactions()) {
reportArea.append(transaction.toString());
reportArea.append("\n");
}
Loops and types are an essential part of programming. You shouldn't use Swing if you don't understand loops and types.
循环和类型是编程的重要组成部分。如果您不了解循环和类型,则不应使用 Swing。
Also, please respect the Java naming conventions. Variables start with a lower-case letter, and don't contain underscore. They're camelCased.
另外,请尊重 Java 命名约定。变量以小写字母开头,不包含下划线。他们是驼峰式的。
回答by VishalDevgire
If you want to append content of objects in ArrayListto JTextAreayou can use this :
如果你想将对象的内容附加ArrayList到JTextArea你可以使用这个:
for (Object obj : arrayList) {
textArea.append(obj.toString() + "");
}
回答by Sane
You have to implement and override toString for TransactionObject.
您必须为 TransactionObject 实现和覆盖 toString。

