2017-06-11 11 views
-2
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 

class Notepad implements ActionListener { 
    Frame f; 
    MenuBar mb; 
    Menu m1, m2; 
    MenuItem nw, opn, sve, sveas, ext, fnd, fr; 
    TextArea t; 

    // [...Constructor removed...] 

    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == nw) { 
      t.setText(" "); 
     } else if (e.getSource() == opn) { 
      try { 
       FileDialog fd = new FileDialog(this, "Open File", FileDialog.LOAD); // <- Does not compile 
       fd.setVisible(true); 
       String dir = fd.getDirectory(); 
       String fname = fd.getFile(); 
       FileInputStream fis = new FileInputStream(dir + fname); 
       DataInputStream dis = new DataInputStream(fis); 
       String str = " ", msg = " "; 
       while ((str = dis.readLine()) != null) { 
        msg = msg + str; 
        msg += "\n"; 
       } 
       t.setText(msg); 
       dis.close(); 
      } catch (Exception ex) { 
       System.out.print(ex.getMessage()); 
      } 
     } 

    } 
    // [...] 
} 

エラー:のFileDialog(メモ帳、文字列、int型)が見つかりません、適切なコンストラクタ私が手

error: no suitable constructor found for FileDialog(Notepad,String,int) 
     FileDialog fd=new FileDialog(this,"Open File",FileDialog.LOAD); 

答えて

1
FileDialog fd=new FileDialog(this,"Open File",FileDialog.LOAD); 
あなたが最初のパラメータとして thisを使用している

、およびthisをするので、あなたが現在作業しているクラスのインスタンスを参照a Notepad。あなたが使用している場合たとえば、どこか他のあなたのコードで:

Notepad np = new Notepad(); 
//... 
np.actionPerformed(ae); //ae is an ActionEvent 

その後thisnpを指します。別のユーザーは非常によく似た答えを私に先行し、申し訳ありません

:あなたは

FileDialog fd=new FileDialog(f,"Open File",FileDialog.LOAD); 

EDITを使用する必要があります

関連する問題