package java1; //使用時這個改成你的包名即可
//文字區 JTextArea
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Areatext extends JFrame implements ActionListener{
JTextArea txt1Area = new JTextArea(7,35);
JTextField txt2Field = new JTextField(35);
String string = "窗外飄起濛濛小雨,\n平添一夜寒意,"+
"\n多少的思緒藏在心底";
public Areatext() {
setSize(400,300);
setVisible(true);
setTitle("文字元件例項");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
txt1Area.setText(string);
add(txt1Area);
add(txt2Field);
validate();
txt2Field.addActionListener(this);//把文字框設定為監聽物件
}
public void actionPerformed(ActionEvent e)
{
String string1 = txt2Field.getText();
txt1Area.append("\n"+string1);
}
}
public class JText {
public static void main(String[] args) {
new Areatext();
}
}