-2
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;

public class two extends JFrame implements ActionListener
{


JLabel l1,l2;
    JTextArea a1,a2;
    JButton b1;
    JMenuBar bar;
    JMenu menu;
    JMenuItem m1,m2;
    JPanel p;
    ArrayList<String> a;
    DataOutputStream d1;

//程序的gui部分

public void go()
{
    p=new JPanel();
    bar=new JMenuBar();
    menu=new JMenu("File");
    m1=new JMenuItem("Save");
    m1.addActionListener(this);
    m2=new JMenuItem("New");
    menu.add(m1);
    menu.add(m2);


Font f=new Font("Arial",Font.BOLD,16);
l1=new JLabel("Question ");
l1.setAlignmentX(CENTER_ALIGNMENT);
l1.setFont(f);
l2=new JLabel("Answer");
l2.setFont(f);
l2.setAlignmentX(RIGHT_ALIGNMENT);
a1=new JTextArea(10,50);
JScrollPane a4 = new JScrollPane(a1);
a4.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
a4.setMaximumSize(new Dimension(600,220));
a4.setAlignmentX(CENTER_ALIGNMENT);
a2=new JTextArea(10,50);
JScrollPane a3 = new JScrollPane(a2);
a3.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
a3.setMaximumSize(new Dimension(600,220));
a3.setAlignmentX(CENTER_ALIGNMENT);
b1=new JButton("Next Card");
b1.setAlignmentX(LEFT_ALIGNMENT);
b1.addActionListener(this);

bar.add(menu);
setJMenuBar(bar);
BoxLayout g=new BoxLayout(p,BoxLayout.Y_AXIS);
p.setLayout(g);
p.add(l1);
p.add(Box.createRigidArea(new Dimension(15,15)));
p.add(a4);
p.add(Box.createRigidArea(new Dimension(15,15)));
p.add(l2);
p.add(Box.createRigidArea(new Dimension(15,15)));
p.add(a3);
p.add(Box.createRigidArea(new Dimension(25,25)));
p.add(b1);
getContentPane().add(p);
setVisible(true);
setSize(500,600);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

public static void main(String[] args) {
    two obj=new two();
    obj.go();
}


public void actionPerformed(ActionEvent e)
{
 a=new ArrayList<String>();

//文本区域内容保存到listarray的部分

 if(e.getSource()==b1)
{
String l=new String(a1.getText()+"/");
String w=new String (a2.getText()+"\n");
a.add(l);
a.add(w);

//值被添加到arraylist,如a.size()的值所示

System.out.println(a.size());
a1.setText("");
a2.setText("");

}

//使用filechooser将listarray保存到文件的方法

if(e.getSource()==m1){

JFileChooser fileSave = new JFileChooser();
int retrival=fileSave.showSaveDialog(this);
File d=fileSave.getSelectedFile();

if (retrival == JFileChooser.APPROVE_OPTION) {

        try{
        System.out.println(d);

//这部分没有被调用

        d1=new DataOutputStream(new FileOutputStream(d+".txt");

        for(String o:a)
        {
        d1.writeChars(o);
     System.out.println("\nIS SUCCESFULLY WRITTEN INTO FILE!");
    }
        d1.close();


    }catch(Exception ex){System.out.println(ex);}
}
}

}
}
4

1 回答 1

1

DataOutputStream is working as expected.

Nothing is written to file as a new ArrayList is created which replaces the previous instance a when actionPerformed is invoked.

a = new ArrayList<String>();

clearing the contents of any previous data so nothing is written to file. Move this statement to the constructor so that the ArrayList is only initialized once. Use clear to clear the List.

Aside: JTextArea has access to the write method which allows its content to be written directly to file

于 2013-07-09T09:32:40.980 回答