你是要将文本域中的内容添加到文本区的后面吧?
*****************************************************************
新建类TextTrans.java,代码如下:
*****************************************************************
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TextTrans extends JFrame {
JTextField tf;
JTextArea ta;
JButton b;
public TextTrans() {
tf = new JTextField(20);
ta = new JTextArea(5, 20);
b = new JButton(" 确定 ");
this.add(tf, BorderLayout.NORTH);
this.add(ta, BorderLayout.CENTER);
this.add(b, BorderLayout.SOUTH);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.append(tf.getText() + "\n");
}
});
}
public static void main(String[] args) {
new TextTrans();
}
}
*****************************************************************
运行结果如下:
*****************************************************************
import java.awt.*;
import java.awt.event.*;
class TestCopy implements ActionListener{
Frame f;
TextField tf=new TextField(30);
Button b=new Button("确定");
TextArea ta=new TextArea("",10,20,TextArea.SCROLLBARS_VERTICAL_ONLY);
Panel p=new Panel(new FlowLayout(FlowLayout.CENTER));
TestCopy(String s){
f=new Frame(s);
b.addActionListener(this);
tf.addActionListener(this);//按回车也可以
p.add(tf);p.add(b);
f.setLayout(new BorderLayout());
f.add(p,BorderLayout.NORTH);
f.add(ta,BorderLayout.CENTER);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e1){
System.exit(0);
}
});
f.setLocation(300,200);
f.setSize(300,200);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b||e.getSource()==tf){
ta.append(tf.getText()+"\n");
tf.setText("");//将TextField置空,不需要就去掉
}
}
public static void main (String[] args) {
new TestCopy("标题照片挡住了");
}
}
图: