Mis*_*lue 0 java swing arraylist jtextarea
我花了一整天的时间在网上和这个网站上寻找我的问题的答案,并希望你们能提供帮助.首先,当我选择"报告"时,我试图将a的内容显示ArrayList为a .数组列表位于与文本区域分开的另一个类中.我的问题源于数组列表是一个对象数组的事实,所以当我尝试显示它时,我得到错误:JTextAreaJButton
The method append(String) in the type JTextArea is not applicable
for the arguments (ArrayList.Account.TransactionObject>)
Run Code Online (Sandbox Code Playgroud)
我可以在控制台窗口中显示数组列表,但是在文本区域中显示它时却很难过.我假设必须存在将Object转换为String的某种问题,因为我无法将其转换为String或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;
}
Run Code Online (Sandbox Code Playgroud)
这是方法(从JB_reportAction上面显示的侦听器类调用),我尝试在文本区域(也在AccountUI类中)显示数组列表:
/**
* Method used to display account transaction history in the text field.
*/
protected void reportAccount()
{
reportArea.append(A.getTransactions());
}
Run Code Online (Sandbox Code Playgroud)
这是Account类中的方法,我能够在控制台输出中显示Array内容,但是无法弄清楚如何将AccountUIString 内容作为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;
}
Run Code Online (Sandbox Code Playgroud)
我希望我在不混淆任何人的情况下澄清了我的问题.任何见解都会非常感激.
呼叫toString()列表:
reportArea.append(A.getTransactions().toString());
Run Code Online (Sandbox Code Playgroud)
或者,如果要以不同的格式显示列表的元素,请遍历元素:
for (TransactionObject transaction : A.getTransactions()) {
reportArea.append(transaction.toString());
reportArea.append("\n");
}
Run Code Online (Sandbox Code Playgroud)
循环和类型是编程的重要部分.如果您不了解循环和类型,则不应使用Swing.
另外,请尊重Java命名约定.变量以小写字母开头,不包含下划线.他们是camelCased.
| 归档时间: |
|
| 查看次数: |
13419 次 |
| 最近记录: |