我正在创建一个GUI,允许用户输入佛罗里达州的湖泊信息,然后才能显示湖泊信息。我希望显示信息位于JOptionPane.showMessageDialog中,该对话框可以滚动所有湖名的ArrayList。我能够将湖泊添加到ArrayList中,但它们不会显示在我的JOptionPane中,并且它是空白的。我知道它正在读取ArrayList中的某些内容,因为它正在打开该窗口。下面是代码片段中的代码,因为整个事情都是疯狂的。在具有滚动功能的JTextArea中显示数组
public static ArrayList<Lakes> lake = new ArrayList<Lakes>();
private JTextArea textAreaDisplay;
private JScrollPane spDisplay;
// this is called in my initComponent method to create both
textAreaDisplay = new JTextArea();
for (Object obj : lake)
{
textAreaDisplay.append(obj.toString() + " ");
}
spDisplay = new JScrollPane(textAreaDisplay);
textAreaDisplay.setLineWrap(true);
textAreaDisplay.setWrapStyleWord(true);
spDisplay.setPreferredSize(new Dimension(500, 500));
// this is called in my createEvents method. After creating lakes in the database
// it will display the else statement but it is empty
btnDisplayLake.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
if (lake.size() == 0)
{
JOptionPane.showMessageDialog(null, "No Lakes in database!");
}
else
JOptionPane.showMessageDialog(null, spDisplay, "Display Lakes", JOptionPane.YES_NO_OPTION);
}
catch (Exception e1)
{
}
}
});
谢谢你提供的任何帮助。我已经在这方面花了几天脑筋。能够完成其他的事情,但回到这个问题。
1)为了更好地提供帮助,请发布[MCVE]或[简短自包含正确示例](http://www.sscce.org/)。 2)不要忽视例外!他们告诉我们到底出了什么问题。除非实现日志记录,否则至少要调用'Throwable.printStackTrace()' –