JTextArea文本不可见且无滚动

guy*_*sei 0 java eclipse swing

第一次用Java编写GUI

http://i58.tinypic.com/s13gh0.png

到目前为止,当我单击"坐标异常"时,JTextArea中会显示字符串的字符串,但查看文本的唯一方法是突出显示它.

我也尝试在JTextArea中添加滚动条,但没有运气.

JTextArea textArea = new JTextArea();
textArea.setBounds(10, 79, 172, 339);
frame.getContentPane().add(textArea);

JButton btnNewButton_1 = new JButton("Coordinate Anomalies");

btnNewButton_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        ArrayList<String> anomalies = vessels.coordinateAnomaly();
        JScrollPane jp = new JScrollPane();
        jp.setViewportView(textArea);

        for (String a : anomalies) {
            textArea.append(a + "\n");
        }

        textArea.setBounds(10, 79, 172, 339);
        frame.getContentPane().add(textArea);
    }
});

btnNewButton_1.setBounds(10, 45, 172, 23);
frame.getContentPane().add(btnNewButton_1);
Run Code Online (Sandbox Code Playgroud)

这是我的代码(抱歉jank),不能强调我是GUI的新手,任何建议都非常感激.

Rei*_*eus 5

组件只能有一个父组件.在应用程序启动时添加滚动窗格而不是JTextArea

frame.add(new JScrollPane(textArea));
Run Code Online (Sandbox Code Playgroud)