将字符串附加到textArea并不做任何事情

时间:2016-02-04 12:21:20

标签: java oop

我正在尝试向textArea添加一个String,它使用自定义showMessage方法作为参数传递。代码运行但我没有在textArea中看到任何文本。这有什么问题?

这是我的实施:

import java.awt.*;
import javax.swing.*;

public class FrameDemo extends JPanel {
    JTextArea textArea;
    private final static String newline = "\n";

    public FrameDemo() {
        super(new GridBagLayout());



        textArea = new JTextArea(5, 20);
        textArea.setEditable(false);


        JScrollPane scrollPane = new JScrollPane(textArea);

        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        add(scrollPane, c);
    }



    private static void createAndShowGUI() {

        JFrame frame = new JFrame("TextDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        frame.add(new FrameDemo());

        frame.pack();
        frame.setVisible(true);
    }


    public static void showMessage(String message){
        FrameDemo text = new FrameDemo();
        text.textArea = new JTextArea(10,10);
        text.textArea.append(message + newline);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();


                showMessage("Hello!");
                showMessage("Hello!");
                showMessage("Hello!");
                showMessage("Hello!");
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

每次执行FrameDemo text = new FrameDemo();时,您都会创建一个新实例(所以新的JTextArea)。 text.textArea = new JTextArea(10,10);

也是如此

可能的解决方案是:

// ....

public void showMessage(String message){ // no longer static
    this.textArea.append(message + newline);
}

// ....

public static void main(String[] args) {
    final FrameDemo text = new FrameDemo();
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();

            text.showMessage("Hello!");
            text.showMessage("Hello!");
            text.showMessage("Hello!");
            text.showMessage("Hello!");
        }
    });
}

注意:未经测试

另请注意,在常量名称中使用大写是常见的惯例(因此newline应为NEWLINE

答案 1 :(得分:0)

如果你不了解基础知识,那就更简单了:

package framedemo;

import javax.swing.*;

public class FrameDemo {

    JTextArea textArea;
    private final static String newline = "\n";

    public FrameDemo() {
        textArea = new JTextArea(5, 20);
        textArea.setEditable(false);        
    }

    public void createAndShowGUI() {
        JFrame frame = new JFrame("TextDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(textArea);
        frame.pack();
        frame.setVisible(true);
    }


    public void showMessage(String message){
        textArea.append(message + newline);
    }

    public static void main(String[] args) {
        FrameDemo frameDemo = new FrameDemo();
        frameDemo.createAndShowGUI();
        frameDemo.showMessage("Hello!");
        frameDemo.showMessage("World!");
    }

}