JTextArea附加新行无效

时间:2013-11-24 15:13:34

标签: java swing newline jtextarea

将“/ n”附加到jtextarea使用应该创建一个新行。但就我而言,它只是打印出来。我该如何解决这个问题?

这是我的代码:

package main;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ConsoleWindow {
static JTextArea out = new JTextArea(5, 50);
final static String newline = "/n";
public static void openWindow(){
    JFrame f = new JFrame("Developer console");
    JTextField in = new JTextField(50);
    JButton b = new JButton("GO");
    JPanel p1 = new JPanel(new GridLayout(1,1,10,10));
    JPanel p2 = new JPanel(new GridLayout(2,1,10,10));

    p1.add(out);
    p2.add(in);
    p2.add(b);

    b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            printText();
        }
    });

    f.setLayout(new GridLayout(2,1,10,10));
    f.add(p1);
    f.add(p2);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setResizable(false);
    f.pack();
    f.setVisible(true);
}
public static void printText(){
    out.append("Hello "+newline);
    out.append("Hello123 "+newline);
}
}

1 个答案:

答案 0 :(得分:4)

替换

final static String newline = "/n";

通过

final static String newline = "\n";