本篇內容介紹了“如何使用JAVA寫文本編輯器”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
再來分析一下,最后一個Menu,里面有幾個按鈕,不知道大家發現沒有,有兩個還是特別簡單,一個是新建,一個是退出。新建我們再實例化一下父窗口就可以了,但是這里有bug,關閉任一子窗口父窗口都會跟著關掉。另一個是退出,直接dispose()就好了。在監聽器里處理一下:
這里就不需要貼太多上下文代碼了,找到主窗口.java 找到該函數就可以
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == item_about) {
new about_Window();
}else if (e.getSource() == item_word_format) {
new about_Format();
}else if (e.getSource() == item_new) {
new test5(); // 選中新建 new一個新窗口 ,有bug,關閉任意子窗口父窗口也會跟著關閉
}else if (e.getSource() == item_exit) {
this.dispose();
}
}在JAVA寫文本編輯器(一)我們有分析過,有一個封裝好的工具JFileChooser可以直接調用。
其實消化完超鏈接里的這篇組件介紹,對于文件的存取已經沒什么問題了。接下來我們添加監聽器,監聽器里添加對應的方法:
當然要先在類內聲明JFileChooser
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == item_about) {
new about_Window();
}else if (e.getSource() == item_word_format) {
new about_Format();
}else if (e.getSource() == item_new) {
new test5(); // 選中新建 new一個新窗口 ,有bug,關閉任意子窗口父窗口也會跟著關閉
}else if (e.getSource() == item_exit) {
this.dispose();
}else if (e.getSource() == item_open) {
openFile();
}else if (e.getSource() == item_save) {
saveFile();
}
}SaveFile方法:
private void saveFile() {
File file = null;
int result ;
fileChooser = new JFileChooser("C:\\");
fileChooser.setApproveButtonToolTipText("保存"); // 設置確認按鈕的現實文本
fileChooser.setDialogTitle("保存文件"); // 設置title
result = fileChooser.showOpenDialog(rootPane); // 設置Dialog的根View 根布局
//--------------------------------------------------------------------------
if(result == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile(); // 若點擊了確定按鈕,給file填文件路徑
}
//--------------------------------------------------------------------------
/*FileOutputStream fileOutputStream = null; // 文件io類
if (file != null) {
try {
fileOutputStream = new FileOutputStream(file);
}catch (FileNotFoundException e) {
e.printStackTrace();
}
String content = edit_text_area.getText();
try {
fileOutputStream.write(content.getBytes());
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fileOutputStream!=null) {
fileOutputStream.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}*/
//---------------這里有嚴重bug,對于字符寫入文件沒問題,但是在讀取中文字符的時候會出現亂碼-----------
//--------------------------------------------------------------------------
try{
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file),"UTF-8"); // 對字符進行編碼轉換
BufferedWriter writer = new BufferedWriter(write);
String content = edit_text_area.getText();
writer.write(content);
writer.close();
}catch(IOException e) {
e.printStackTrace();
}
}OpenFile方法:
/**
* 點擊新建按item時 打開JFileChooser對話框
* 并且對文件讀取進行處理
*/
private void openFile() {
File file = null;
int result ;
fileChooser = new JFileChooser("C:\\");
fileChooser.setApproveButtonToolTipText("確定"); // 設置確認按鈕的現實文本
fileChooser.setDialogTitle("打開文件"); // 設置title
result = fileChooser.showOpenDialog(rootPane); // 設置Dialog的根View 根布局
//--------------------------------------------------------------------------
if(result == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile(); // 若點擊了確定按鈕,給file填文件路徑
}
//--------------------------------------------------------------------------
//--------------------下面對文件進行處理,把內容裝到父窗體的textarea中--------------------
/*FileInputStream fileInputStream = null;
if (file != null) {
try { //此處需要注意空指針異常 即沒有找到文件的時候需要處理
fileInputStream = new FileInputStream(file); // 將file文件的數據流裝到fileInputStream里
}catch (FileNotFoundException e) { // 捕獲到異常 ,需要處理
e.printStackTrace(); // 將異常實例化為e 然后在控制臺Console 打印出錯誤的位置和原因
TipDialog tmpDialog = new TipDialog(this,"錯誤文件",true,"文件夾名稱錯誤,請重新檢查!");// 此處我們還可以對一場做一些處理,在這里彈出一個警示對話框
}
//讀取文件
int readbyte ;
try {
while ((readbyte = fileInputStream.read())!=-1) { //一段段的讀取文件
edit_text_area.append(String.valueOf((char)readbyte)); //在editarea 里一行行添加
}
}catch (IOException e) { // 處理異常
e.printStackTrace();
}finally {
try {
if (fileInputStream != null) { //對fileInputStream 回收
fileInputStream.close();
}
}catch (IOException e) { //拋出異常
e.printStackTrace();
}
}
}*/
//---------------這里有嚴重bug,對于讀取中文字符會出現亂碼-------------------------------
//--------------------------------------------------------------------------
if(file.isFile() && file.exists()) {
BufferedReader reader = null;
try {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),"UTF-8");
reader = new BufferedReader(inputStreamReader);
String readLine = "";
while ((readLine = reader.readLine()) != null) { // 對BufferedReader數據一行行讀
//edit_text_area.append(readLine); 這樣寫會出現所有的句子都出現在同一行的情況,所以在每次append的時候在后面加一個換行符
edit_text_area.append(readLine+'\n'); //對edit_text_area 一行行加
}
reader.close(); // 關閉reader
}catch (IOException e) {
e.printStackTrace();
//TipDialog tmpDialog = new TipDialog(this,"錯誤文件",true,"文件夾名稱錯誤,請重新檢查!");
}
}
}其實這里的兩個方法都很相似,通過FileChooser可以得到選中的文件的路徑,然后通過File 把路徑拿到,在執行一下常規的文件讀寫操作。注意一定要處理IO操作異常。
如果大家在注意的話可以看到其實我的IO是有一些操作注釋掉的,注釋掉的部分是按照使用JFileChooser組件寫的讀寫操作,但是經過測試,發現對于中文字符沒有編碼,所以存入之后再讀取就是亂碼。所以采用了另外一種寫法。
“如何使用JAVA寫文本編輯器”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。