/**************************************************/
//調用的函數
public void File_new_actionPerformed(ActionEvent e){
FileNew();
}
public void File_open_actionPerformed(ActionEvent e){
FileOpen();
}
public void File_save_actionPerformed(ActionEvent e){
FileSave();
}
public void File_saveto_actionPerformed(ActionEvent e){
FileSaveTo();
}
public void File_close_actionPerformed(ActionEvent e){
FileClose();
}
public void mouseReleased(MouseEvent e){
if(e.isPopupTrigger())
pm.show(this,e.getX(),e.getY());
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
boolean toExit()
{
if(bsaved)
return true;
int i=JOptionPane.showConfirmDialog(this,"是否保存文件","Jishiben",JOptionPane.YES_NO_CANCEL_OPTION);
switch(i)
{
case JOptionPane.YES_OPTION:
return true;
case JOptionPane.NO_OPTION:
return false;
case JOptionPane.CANCEL_OPTION:
return false;
default:
return false;
}
}
//調用文件新建
void FileNew()
{
if(bsaved){
this.jTextArea1.setText("");
this.jTextArea1.setFocusable(true);
this.setTitle("未命名");
this.statubar.setText("新建文本");
}
else FileSaveTo();
}
//調用文件打開
void FileOpen()
{
String strFileOpen="";
if(bsaved){
try{
if(this.jFileChooser.APPROVE_OPTION==this.jFileChooser.showOpenDialog(this)){
strFileOpen=this.jFileChooser.getSelectedFile().getPath();
File file=new File(strFileOpen);
int flength=(int)file.length();
int num=0;
FileReader fReader=new FileReader(file);
char[] data=new char[flength];
while(fReader.ready()){
num+=fReader.read(data,num,flength-num);
}
fReader.close();
jTextArea1.setText(new String(data,0,num));
filename=strFileOpen;
this.setTitle(filename);
statubar.setText("打開:"+filename);
updateFname();
bsaved=false;
}
else
return ;
}catch(Exception e){
statubar.setText("Error Open:"+e.getMessage());
}
}
else
FileSave();
}
//調用保存對話框
boolean FileSave()
{
if(filename==null){
return FileSaveTo();
}
if(!bsaved){
if(filename.length()!=0){
try{
File saveFile=new File(filename);
FileWriter fw=new FileWriter(saveFile);
fw.write(jTextArea1.getText());
fw.close();
statubar.setText("保存文件:"+filename);
bsaved=true;
updateFname();
}catch(Exception e)
{
statubar.setText("保存出錯: "+e.getMessage());
}
}
else{
return FileSaveTo();
}
}
return true;
}
//調用另存對話框
boolean FileSaveTo()
{
if(jFileChooser.APPROVE_OPTION==jFileChooser.showSaveDialog(this)){
filename=jFileChooser.getSelectedFile().getPath();
return FileSave();
}
else{
return false;
}
}
//更改標題
void updateFname(){
String str;
if(filename==null){
str="無標題";
}
else{
str=filename;
}
if(!bsaved){
str="記事本"+str;
}
this.setTitle(str);
this.repaint();
}
//文件關閉
void FileClose(){
if(!bsaved){
if(toExit()){
FileSave();
}
else {
System.exit(0);
}
}
else{
System.exit(0);
}
}
//調用編輯剪切
void FileCut (ActionEvent e)
{
try{
String str=this.jTextArea1.getSelectedText();
if(str.length()!=0){
StringSelection s = new StringSelection(str);
cb.setContents(s,s);
this.jTextArea1.replaceRange("",this.jTextArea1.getSelectionStart(),this.jTextArea1.getSelectionEnd());
bsaved =false;
}
}catch(Exception ex){
this.statubar.setText("剪切出錯:"+ex.getMessage());
}
}
//調用編輯復制
void FileCopy(ActionEvent e)
{
try{
String str=this.jTextArea1.getSelectedText();
if(str.length()!=0){
StringSelection s = new StringSelection(str);
cb.setContents(s,s);
}
}catch(Exception ex)
{
this.statubar.setText("復制出錯!"+ex.getMessage());
}
}
//調用編輯粘貼
void FilePaste(ActionEvent e)
{
try
{
Transferable tr =cb.getContents(this);
if (tr != null)
{
String s = (String)tr.getTransferData(DataFlavor.stringFlavor);
if(s!=null)
jTextArea1.replaceRange(s,jTextArea1.getSelectionStart(),jTextArea1.getSelectionEnd());
//this.jTextArea1.insert(s,this.jTextArea1.getCaretPosition());
bsaved =false;
}
}
catch (Exception err)
{
err.printStackTrace();
}
}
//調用編輯刪除
void FileDel(ActionEvent e)
{
jTextArea1.replaceRange("",jTextArea1.getSelectionStart(),jTextArea1.getSelectionEnd());
bsaved =false;
}
//調用編輯查找
void FileFindNext(ActionEvent e)
{
new Frame1(jTextArea1);
}
//時間
void FileTime(ActionEvent e){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
jTextArea1.append(" 當前