| 22 | */ |
| 23 | public class TextDemo2 { |
| 24 | public static void main(String[] args) { |
| 25 | // ����������� |
| 26 | Frame f = new Frame("����ת��"); |
| 27 | // �������� |
| 28 | f.setBounds(500, 250, 400, 300); |
| 29 | // ���� |
| 30 | f.setLayout(new FlowLayout()); |
| 31 | |
| 32 | // �����ı��� |
| 33 | final TextField tf = new TextField(20); |
| 34 | // ������ť |
| 35 | Button bu = new Button("д���ı��ļ�"); |
| 36 | Button bu2 = new Button("��ȡ�ı��ļ�"); |
| 37 | // �����ı��� |
| 38 | final TextArea ta = new TextArea(10, 40); |
| 39 | |
| 40 | // ���ӵ����� |
| 41 | f.add(tf); |
| 42 | f.add(bu); |
| 43 | f.add(bu2); |
| 44 | f.add(ta); |
| 45 | |
| 46 | // ע���¼� |
| 47 | f.addWindowListener(new WindowAdapter() { |
| 48 | @Override |
| 49 | public void windowClosing(WindowEvent e) { |
| 50 | System.exit(0); |
| 51 | } |
| 52 | }); |
| 53 | |
| 54 | // ��bu�����¼� |
| 55 | bu.addActionListener(new ActionListener() { |
| 56 | @Override |
| 57 | public void actionPerformed(ActionEvent e) { |
| 58 | // ��ȡ�ı����ֵ����ֵ���ı��� |
| 59 | String tf_str = tf.getText(); |
| 60 | // ������� |
| 61 | tf.setText(""); |
| 62 | |
| 63 | // д���ı��ļ� |
| 64 | BufferedWriter bw = null; |
| 65 | try { |
| 66 | bw = new BufferedWriter(new FileWriter("bw.txt", true)); |
| 67 | bw.write(tf_str); |
| 68 | bw.newLine(); |
| 69 | bw.flush(); |
| 70 | } catch (IOException e1) { |
| 71 | e1.printStackTrace(); |
| 72 | } finally { |
| 73 | if (bw != null) { |
| 74 | try { |
| 75 | bw.close(); |
| 76 | } catch (IOException e1) { |
| 77 | e1.printStackTrace(); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |