一個簡單的記事本代碼(二)

來源:互聯網
上載者:User

/**************************************************/
//調用的函數
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(" 目前時間:"+sdf.format(new Date()));
bsaved =false;
}
//全選
void AllSelect(ActionEvent e){
jTextArea1.setSelectionStart(0);
jTextArea1.setSelectionEnd(this.jTextArea1.getText().length());
}
//換行
void FileLine(ActionEvent e){
if(!jTextArea1.getLineWrap()){
jTextArea1.setLineWrap(true);
}
else{
jTextArea1.setLineWrap(false);
}
} //有問題
// 顏色
public void fcolor(ActionEvent e){
Color bcolor=jTextArea1.getForeground();
jColor.setColor(bcolor);
jTextArea1.setForeground(
jColor.showDialog(jTextArea1,"選擇顏色",bcolor));
}
//調用關於對話方塊
void HelpAboutDialog(ActionEvent e)
{
JOptionPane.showMessageDialog(this,"著作權:龍骨敗..","關於記事本",JOptionPane.INFORMATION_MESSAGE);
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.