下面图老师小编跟大家分享一个简单的记事本代码02,一起来学习下过程究竟如何进行吧!喜欢就赶紧收藏起来哦~
【 tulaoshi.com - 编程语言 】
/**************************************************/(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)
//调用的函数
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);
}
来源:http://www.tulaoshi.com/n/20160219/1609121.html
看过《一个简单的记事本代码02》的人还看了以下文章 更多>>