想模拟一个聊天的小程序,可在一开始就有个问题不知道怎么解决,网上找了半天,也没有什么好的解决方法,还是得麻烦大家一下。
问题描述:我想把输入到TextField中的字符串通过点击“发送”或者敲回车键使其显示到TextArea中,然后TextField清空。问题是每次TextField中的字符串都会覆盖TextArea中之前的字符串,而我想使所有的字符串都分行显示出来。就想qq聊天的界面一样。这个该怎么做呢,麻烦大家给点提示,谢谢。
我的代码:package com.chat.lv;
import java.awt.*;
import java.awt.event.*;public class ChatClient extends Frame{
TextField tf = new TextField(53);
TextArea ta = new TextArea();
Button bt = new Button("发送");
Panel p1 = new Panel();
Panel p2 = new Panel();
static int i = 0;
public void launchFrame(){
setBounds(300,300,300,300);
//setLayout(new GridLayout(2,1));
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
p1.add(ta);
p2.setLayout(new FlowLayout());
p2.add(tf);
p2.add(bt);
pack();
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
bt.addActionListener(new TFAction());
tf.addActionListener(new TFAction());
}
class TFAction implements ActionListener{
public void actionPerformed(ActionEvent e) {
String s = tf.getText();
ta.setText("第" + i++ + "行:"+s);
tf.setText("");
}
}
public static void main(String[] args) {
new ChatClient().launchFrame();
}
}
问题描述:我想把输入到TextField中的字符串通过点击“发送”或者敲回车键使其显示到TextArea中,然后TextField清空。问题是每次TextField中的字符串都会覆盖TextArea中之前的字符串,而我想使所有的字符串都分行显示出来。就想qq聊天的界面一样。这个该怎么做呢,麻烦大家给点提示,谢谢。
我的代码:package com.chat.lv;
import java.awt.*;
import java.awt.event.*;public class ChatClient extends Frame{
TextField tf = new TextField(53);
TextArea ta = new TextArea();
Button bt = new Button("发送");
Panel p1 = new Panel();
Panel p2 = new Panel();
static int i = 0;
public void launchFrame(){
setBounds(300,300,300,300);
//setLayout(new GridLayout(2,1));
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
p1.add(ta);
p2.setLayout(new FlowLayout());
p2.add(tf);
p2.add(bt);
pack();
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
bt.addActionListener(new TFAction());
tf.addActionListener(new TFAction());
}
class TFAction implements ActionListener{
public void actionPerformed(ActionEvent e) {
String s = tf.getText();
ta.setText("第" + i++ + "行:"+s);
tf.setText("");
}
}
public static void main(String[] args) {
new ChatClient().launchFrame();
}
}
将给定文本追加到文本区的当前文本。
我想的是让每一次输入的字符串都分行显示出来。我在每次ta.setText()时加入换行符好像也不行,
他会把换行符直接显示出来。
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;public class TextAreaExample {
public static void main(final String... args) {
EventQueue.invokeLater(new Runnable(){
@Override public void run() {
final JFrame frame = new JFrame("^_^");
final JTextArea history = new JTextArea();
final JTextField input = new JTextField();
frame.add(new JScrollPane(history), BorderLayout.CENTER);
frame.add(input, BorderLayout.PAGE_END); final Action send = new AbstractAction("Send"){
@Override public void actionPerformed(final ActionEvent e){
String message = input.getText();
history.append(String.format("%s%n", message));
input.setText("");
}
};
input.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "SEND");
input.getActionMap().put("SEND", send); frame.setPreferredSize(new Dimension(400,300));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
我想的是让每一次输入的字符串都分行显示出来。我在每次ta.setText()时加入换行符好像也不行,
他会把换行符直接显示出来。
分行显示append("\n")
我想的是让每一次输入的字符串都分行显示出来。我在每次ta.setText()时加入换行符好像也不行,
他会把换行符直接显示出来。
分行显示append("\n")append("/n")的话,会把/n显示出来,而没有换行。
我想的是让每一次输入的字符串都分行显示出来。我在每次ta.setText()时加入换行符好像也不行,
他会把换行符直接显示出来。
分行显示append("\n")append("/n")的话,会把/n显示出来,而没有换行。
是"\n"不是"/n"
str=str.replaceAll("\r\n", "<br>");