?? app17_9.java
字號:
// app17_9, FileDialog類的使用
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class app17_9 extends Frame implements ActionListener
{
static app17_9 frm=new app17_9();
static FileDialog fdlg=new FileDialog(frm,"Open"); // 建立fdlg對象
static Button btn=new Button("Open");
static TextArea txa=new TextArea();
public static void main(String args[])
{
BorderLayout br=new BorderLayout();
frm.setTitle("Dialog Demo");
frm.setLayout(br);
frm.setSize(200,150);
frm.add(txa,br.CENTER);
frm.add(btn,br.SOUTH);
btn.addActionListener(frm); // 設置frm為btn的事件聆聽者
frm.setVisible(true);
}
// 當Open按鈕按下時,執行下列的程序代碼
public void actionPerformed(ActionEvent e)
{
fdlg.setVisible(true); // 顯示文件對話框
// 以下的程序代碼是按下文件對話框內的“打開”鈕才會執行
String fname=fdlg.getDirectory()+fdlg.getFile();// 取得路徑與名稱
try{
FileInputStream fi=new FileInputStream(fname);
byte ba[]=new byte[fi.available()];
fi.read(ba); // 讀入文件內容到byte數組里
txa.setText(new String(ba)); // 將byte數組的內容寫到txa控件里
fi.close();
}
catch(IOException ioe){};
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -