エディタやワープロでやるように,ファイルの一覧を出してその中からファイルを選択するようにします。
JFileChooser
を使います。
前回の構成
public SanTaku(){ setDefaultCloseOperation(EXIT_ON_CLOSE); m = new Mondaishuu("mondai.txt",10 );
今回の構成
public SanTakuF(){ setDefaultCloseOperation(EXIT_ON_CLOSE); JFileChooser chooser = new JFileChooser("./"); int retVal = chooser.showOpenDialog(this); if (retVal != JFileChooser.APPROVE_OPTION) System.exit(0); File file = chooser.getSelectedFile(); String fname = file.getPath(); m = new Mondaishuu(fname,10);
なかにFileクラスを使っているので、次のパッケージをインポートする必要があります。
import java.io.*;