-1

.txt ファイルを検索するプログラムを作成しました。

ファイルをクリックすると、[プログラムから開く] ダイアログ ボックスが表示され、そのダイアログ ボックスには、インストールされているすべてのプログラムの一覧が表示されます。

ファイルを検索するためにこのコードを使用しています:

  public File[] finder( String dirName)
  {
      // Create a file object on the directory.
      File dir = new File(dirName);
      // Return a list of all files in the directory.
      return dir.listFiles(new FilenameFilter();
  } 

  public boolean accept(File dir, String filename)
  { 
      return filename.endsWith(".txt");
  } 

「開く」ダイアログボックスを表示するには、どの Java コードを使用できますか?

4

3 に答える 3

3

これに使用する必要があります FileChooserここを見てください:

//Create a file chooser
final JFileChooser fc = new JFileChooser();
...
//In response to a button click:
int returnVal = fc.showOpenDialog(aComponent);


public void actionPerformed(ActionEvent e) {
    //Handle open button action.
    if (e.getSource() == openButton) {
        int returnVal = fc.showOpenDialog(FileChooserDemo.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //This is where a real application would open the file.
            log.append("Opening: " + file.getName() + "." + newline);
        } else {
            log.append("Open command cancelled by user." + newline);
        }
   } ...
}
于 2013-05-14T11:51:26.987 に答える
3

「開く」ダイアログボックスを表示するには、どの Java コードを使用できますか?

私の知る限り、J2SE にはそのようなものはありません。OTOH API は、どのアプリでもDesktop開くことができます。デフォルトのコンシューマですFile

于 2013-05-14T11:55:07.233 に答える