?? textfileviewer.java
字號:
/**
* 本案例創建一個包含TextArea區域的窗口,
* 在該區域中顯示打開的文本文件的內容.
**/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class TextFileViewer extends Frame implements ActionListener {
String directory; // 在FileDialog中顯示的默認目錄
TextArea textarea; // 顯示文本文件的區域
// 構造方法: 打開一個文件瀏覽器
public TextFileViewer() {
this(null, null);
}
// 構造方法: 顯示當前目錄下的文件
public TextFileViewer(String filename) {
this(null, filename);
}
// 構造方法: 創建用來顯示指定目錄中的指定文件內容的TextFileViewer對象
public TextFileViewer(String directory, String filename) {
super(); // 創建框架
// 當用戶請求時銷毀窗口
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
// 創建一個用來顯示文件內容的TextArea區域
textarea = new TextArea("", 24, 80);
textarea.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
textarea.setEditable(false);
this.add("Center", textarea);
// 創建一個包含兩個按鈕控件的面板
Panel myPanel = new Panel();
myPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 5));
this.add(myPanel, "South");
// 創建按鈕控件,并且處理單擊按鈕事件
Font font = new Font("SansSerif", Font.BOLD, 14);
Button btnOpenFile = new Button("打開文件");
Button btnClose = new Button("關閉");
btnOpenFile.addActionListener(this);
btnOpenFile.setActionCommand("open");
btnOpenFile.setFont(font);
btnClose.addActionListener(this);
btnClose.setActionCommand("close");
btnClose.setFont(font);
myPanel.add(btnOpenFile);
myPanel.add(btnClose);
this.pack();
// 指明目錄
if (directory == null) {
File f;
if ((filename != null) && (f = new File(filename)).isAbsolute()) {
directory = f.getParent();
filename = f.getName();
} else
directory = System.getProperty("user.dir");
}
this.directory = directory; // 記住目錄
setFile(directory, filename); // 載入并顯示文件
}
//載入并且顯示指定目錄中的指定文件內容
public void setFile(String directory, String filename) {
if ((filename == null) || (filename.length() == 0))
return;
File f;
FileReader in = null;
// 讀取并且顯示文件內容
// 因為是在讀取文本,所以使用FileReader,而不使用FileInputStream.
try {
f = new File(directory, filename); // 創建一個file對象
in = new FileReader(f); // 和一個用來讀取它的字符流
char[] buffer = new char[4096]; // 每次讀取4K字符
int len; // 每次讀入的字符數
textarea.setText(""); // 清除文本區域
while ((len = in.read(buffer)) != -1) { // 讀取一批字符
String s = new String(buffer, 0, len); // 轉化為一個字符串
textarea.append(s); // 顯示文本
}
this.setTitle("TextFileViewer: " + filename); // 設置窗口標題
textarea.setCaretPosition(0); // 設置文件起始位置
}
// 顯示錯誤信息
catch (IOException e) {
textarea.setText(e.getClass().getName() + ": " + e.getMessage());
this.setTitle("TextFileViewer: " + filename + ": I/O Exception");
}
// 關閉輸入流
finally {
try {
if (in != null)
in.close();
} catch (IOException e) {
}
}
}
//處理單擊按鈕事件
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("open")) { // 如果用戶單擊了“打開”按鈕
// 創建一個文件對話框,提示輸入新的文件
FileDialog myFileDialog = new FileDialog(this, "Open File", FileDialog.LOAD);
myFileDialog.setDirectory(directory); // 設置默認目錄
// 顯示對話框并等待用戶的輸入
myFileDialog.show();
directory = myFileDialog.getDirectory(); // 記住新的默認目錄
setFile(directory, myFileDialog.getFile()); // 載入并顯示選擇
myFileDialog.dispose(); // 關閉對話框
} else if (cmd.equals("close")) // 如果用戶單擊了“關閉”按鈕
this.dispose(); //關閉窗口
}
//TextFileViewer可以被其他程序所使用
//也可以加上main()方法,成為一個獨立程序.
static public void main(String[] args) throws IOException {
// 創建一個TextFileViewer對象
Frame myFrame = new TextFileViewer((args.length == 1) ? args[0] : null);
// 當TextFileViewer窗口關閉時,退出
myFrame.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
// 彈出一個窗口
myFrame.show();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -