繁体   English   中英

使用arrayList在JTextArea中显示

[英]Use arrayList to display in JTextArea

我希望能够在JTextArea中显示arrayList中的所有项目。 这是我的代码,但它不起作用。

public void display()
{
    JPanel display = new JPanel();
    display.setVisible(true);
    JTextArea a;

    for (Shape ashape : alist)
    {
        System.out.println("");
        System.out.println(ashape.toString());
        a = new JTextArea(ashape.toString()); 
        display.add(a);
    }

    Container content = getContentPane(); 
    content.add(display);
}

移动

JTextArea a;

在for循环中,像这样:

for (Shape ashape : alist) {
        System.out.println("");
        System.out.println(ashape.toString());

        //initialise a here 
        JTextArea a = new JTextArea(ashape.toString()); 
        display.add(a);
    }

    Container content = getContentPane(); 
    content.add(display);
}

另外,你的程序中“它不起作用”是什么意思?

有几种方法可以实现这一目标。 首先,您的示例代码为每个Shape创建一个新的JTextArea ,但它只是将最后一个添加到UI。

假设您想在单个文本区域中显示所有信息,您可以简单地使用JTextArea#append ,例如

JTextArea a = new JTextArea();

for (Shape ashape : a list)
{
    System.out.println(ashape.toString());
    a.append(ashape.toString() + "\n")
}

Container content = getContentPane(); 
content.add(display);

Ps-您可能希望将文本区域包装在JScrollPane以便它可以溢出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM