?? filemsgprocess.java
字號:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
public class fileMsgProcess implements ActionListener{
menu men;//菜單欄
GUI gui;//主界面
//文件變量
JFileChooser choser=new JFileChooser();
FileInputStream fis;
FileOutputStream fos;
File file;
//構造函數
public fileMsgProcess(menu men,GUI gui)
{
this.gui=gui;
this.men=men;
}
//
public void actionPerformed(ActionEvent e)
{
GUI.quit=true;//確保失去焦點
//新建文件
if(e.getSource()==men.file[0]){
newFile();
}
//打開文件
else if(e.getSource()==men.file[1]){
openFile();
}
//保存文件
else if(e.getSource()==men.file[2]){
saveFile();
}
//另存為
else if(e.getSource()==men.file[3]){
saveAs("另存為...");
}
//退出
else if(e.getSource()==men.file[4]){
exit();
}
}
//***********************
//打開新文件
//***********************
public void newFile()
{
if(saveProcess()){
file=null;
GUI.jt.setText("");
GUI.jf.setTitle("無標題-記事本");
}
}
//***********************
//打開文件
//***********************
public void openFile()
{
//判斷當前文件是否保存
if(!saveProcess()) return;
//打開文件
choser.setDialogType(JFileChooser.OPEN_DIALOG);
choser.setFileFilter(new filefilter(new String[]{"txt"},"文本文件(*.txt)"));
int state= choser.showDialog(null,"打開");
file=choser.getSelectedFile();
if(file!=null && state==JFileChooser.APPROVE_OPTION){
try{
fis=new FileInputStream(file);
GUI.jt.read(fis,gui);
fis.close();
GUI.jf.setTitle(file.getName()+"-記事本");
}
catch(IOException e1)
{
System.err.println(e1);
}
}
}
//***********************
//保存當前文件
//***********************
public boolean saveFile()
{
if(!(file instanceof File)){//文件不存在
return saveAs("保存");
}
else{//文件存在
try{
fos=new FileOutputStream(file);
fos.write(GUI.jt.getText().getBytes());
fos.close();
GUI.jf.setTitle(file.getName()+"-記事本");
}
catch(Exception e1)
{
System.err.println(e1);
}
}
GUI.change=false;
return true;
}
//***********************
//另存為文件
//***********************
public boolean saveAs(String name)
{
choser.setDialogType(JFileChooser.SAVE_DIALOG);
choser.setFileFilter(new filefilter(new String[]{"txt"},"文本文件(*.txt)"));
while(true){
int state=choser.showDialog(null,name);
file=choser.getSelectedFile();
if(file!=null && state==JFileChooser.APPROVE_OPTION && !file.getName().equals("")){
break;
}
else if(state!=JFileChooser.APPROVE_OPTION) return false;
}
try{
file.createNewFile();
fos=new FileOutputStream(file);
fos.write(GUI.jt.getText().getBytes());
fos.close();
GUI.jf.setTitle(file.getName()+"-記事本");
}
catch(Exception e1)
{
System.err.println(e1);
}
GUI.change=false;
return true;
}
//*********************
//退出
//*********************
public void exit()
{
//判斷文件是否保存 保存后退出
if(saveProcess())
{
GUI.quit=true;
System.exit(1);
}
GUI.quit=false;
}
//**************************
//開始其他操作之前
//對于當前文件進行處理
//**************************
public boolean saveProcess()
{
//文件已經修改過
if(GUI.change==true)
{
int i=JOptionPane.showConfirmDialog(null,"是否保存當前文件?","記事本",
JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(i==JOptionPane.CANCEL_OPTION) return false;
else if(i==JOptionPane.NO_OPTION) return true;
else if(i==JOptionPane.YES_OPTION){//需要保存
//判斷當前文件是否已經保存過
return saveFile();
}
}
return true;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -