⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 note.java

📁 JAVA课程设计 简单文本编辑器。适合JAVA初学者做课程设计!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

if(p.getActionCommand()=="保存 "||p.getSource()==save)
{
if(fname==null) //如果文件名为空,说明文件未被创建,弹出另存为对话框
{ othersave(); }
try {
File savefile=new File(path);
savefile.createNewFile(); 
FileWriter fw=new FileWriter(savefile); 
fw.write(txt1.getText()); 
fw.close(); 
}
catch(IOException e)
{}
}

if(p.getActionCommand()=="另保存为 ")
{othersave();}
if(p.getActionCommand()=="退出 "||p.getSource()==exit)
{exit();}
//////////////////编辑////////////////////

if(p.getActionCommand()=="复制 "||p.getSource()==copy)
{ txt1.copy(); }
if(p.getActionCommand()=="剪切 "||p.getSource()==cut)
{ txt1.cut(); }
if(p.getActionCommand()=="粘贴 "||p.getSource()==pp)
{ txt1.paste(); }
if(p.getActionCommand()=="删除 "||p.getSource()==del)
{ 
txt1.replaceSelection("");
}
///////////////////////////////////////////////////////
if(p.getActionCommand()=="查找 "||p.getSource()==findc) // 创建查找对话框;
{find=new Dialog(this,"查找");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
findtxt=new JTextField(7);
p1.add(new JLabel("输入要查找字符:"));
p1.add(findtxt);
p2.add(findenter);
find.add("Center",p1);
find.add("South",p2);
find.setSize(200,100);
find.show();
}
if(p.getSource()==findenter) ///点击查找对话框的确定按钮后的事件处理
{
if(findtxt.getText().equals("")) 
{find.dispose();}
else if(!findtxt.getText().equals(""))
{ find.dispose();
str=txt1.getText();
startp=str.indexOf(findtxt.getText());
endp=startp+findtxt.getText().length();
txt1.select(startp,endp);
m26.setEnabled(true);
newendp=endp;////////获取这次查找的终点
notfindmethod();
}
}
//////////////////////////////////////////////////////////
if(p.getActionCommand()=="查找下一个 ")
{
nexttemp=newendp; /////获取上次查找的终点做为未查找字符串的起点
String strall=txt1.getText();
txt1.select(nexttemp,strall.length()); /////选中所有未查找的字符串
strnext=txt1.getSelectedText();
newstartp=strnext.indexOf(findtxt.getText())+nexttemp;/////在未查找的字符串里搜索对应字符的在TXT1中的位置
newendp=newstartp+findtxt.getText().length();
txt1.select(newstartp,newendp); ////找到相应文本,并选择
notfindmethod();
}
if(p.getActionCommand()=="替换 ")
{m271.setEnabled(true);
replace=new Dialog(this,"替换"); // 创建替换对话框;
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
findtxt=new JTextField(7);
repltxt=new JTextField(7);
p1.add(new JLabel("输入原始字符:"));
p1.add(findtxt);
p2.add(new JLabel("输入替换字符:"));
p2.add(repltxt);
p3.add(replb);
replace.add("North",p1);
replace.add("Center",p2);
replace.add("South",p3);
replace.setSize(200,150);
replace.show();
}
if(p.getSource()==replb) ///点击替换对话框的确定按钮后的事件处理
{
replace.dispose();
str=txt1.getText();
startp=str.indexOf(findtxt.getText());
endp=startp+findtxt.getText().length();
txt1.replaceRange(repltxt.getText(),startp,endp);
newendp=endp; ///获取这次替换的终点
}
if(p.getActionCommand()=="替换下一个")
{nexttemp=newendp; /////获取上次查找的终点做为未查找字符串的起点
String strall=txt1.getText();
txt1.select(nexttemp,strall.length()); /////选中所有未查找的字符串
strnext=txt1.getSelectedText();
newstartp=strnext.indexOf(findtxt.getText())+nexttemp;/////在未查找的字符串里搜索对应字符的在TXT1中的位置
newendp=newstartp+findtxt.getText().length();
txt1.select(newstartp,newendp); 
notfindmethod();
txt1.replaceRange(repltxt.getText(),newstartp,newendp);//替换字符
}
if(p.getActionCommand()=="全选 ")
{txt1.selectAll(); } 

if(p.getActionCommand()=="日期/时间 ") //用DATE类插入当前日期/时间,编译时会出现说明
{ int inpoint=txt1.getCaretPosition();
Date dt=new Date();
String strdate=dt.toLocaleString();
txt1.insert(strdate,inpoint);}
//格式//
//////////字体样式////////////////
if(p.getActionCommand()=="正常 ")
{ newfont=txt1.getFont(); //得到当前应用字体,获得字体大小,创建相应样式
int size=newfont.getSize();
txt1.setFont(new Font("宋体",Font.PLAIN,size)); }
if(p.getActionCommand()=="粗体 ")
{ newfont=txt1.getFont();
int size=newfont.getSize();
txt1.setFont(new Font("宋体",Font.BOLD,size)); }

if(p.getActionCommand()=="斜体 ")
{ newfont=txt1.getFont();
int size=newfont.getSize();
txt1.setFont(new Font("宋体",Font.ITALIC,size)); }

////////////字体大小/////////////////
if(p.getActionCommand()=="最大 ")
{ newfont=txt1.getFont(); ////得到当前应用字体,获得字体样式,创建相应字体的大小
int sty=newfont.getStyle() ;
txt1.setFont(new Font("宋体",sty,35)); }
if(p.getActionCommand()=="较大 ")
{ newfont=txt1.getFont();
int sty=newfont.getStyle() ;
txt1.setFont(new Font("宋体",sty,30)); }
if(p.getActionCommand()=="适中 ")
{ newfont=txt1.getFont();
int sty=newfont.getStyle() ;
txt1.setFont(new Font("宋体",sty,18)); }
if(p.getActionCommand()=="较小 ")
{ newfont=txt1.getFont();
int sty=newfont.getStyle() ;
txt1.setFont(new Font("宋体",sty,16)); }
if(p.getActionCommand()=="最小 ")
{ newfont=txt1.getFont();
int sty=newfont.getStyle() ;
txt1.setFont(new Font("宋体",sty,14)); }

if(p.getActionCommand()=="字体颜色 "||p.getSource()==color) //字体颜色
{
JColorChooser jColor=new JColorChooser(); //调用颜色面板,设置前景就可更改字体颜色
Color fcolor=txt1.getForeground(); 
txt1.setForeground( jColor.showDialog(txt1,"选择字体颜色",fcolor)); 
}

if(p.getActionCommand()=="自动换行 ")
{ txt1.setLineWrap(true);
m34.setEnabled(false);
}



if(p.getActionCommand()=="MOTIF风格 ")
{ this.dispose();
myfr ww=new myfr("我的javaX记事本");
try { //MOTIF界面
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); 
SwingUtilities.updateComponentTreeUI(ww);
}
catch ( Exception e ) {}
}


if(p.getActionCommand()=="默认风格 ")
{
this.dispose();
myfr ww=new myfr("我的javaX记事本");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(ww);
}
catch ( Exception e ) {}
}

if(p.getActionCommand()=="状态栏 ") //状态栏的隐藏和显视
{state.setVisible( !(state.isVisible()) );}

/////////////////////帮助///////////////////////////////////////////
if(p.getActionCommand()=="帮助主题 "||p.getSource()==help)
{myhelp help=new myhelp();
help.setSize(200,240);}

if(p.getActionCommand()=="关于 ")
{ 
JLabel prompt=new JLabel("欢迎使用我的JAVAX记事本",JLabel.CENTER);

JOptionPane.showMessageDialog(null,prompt,"关于我的JAVAX记事本",JOptionPane.INFORMATION_MESSAGE);
}
}

public void othersave() //另存为方法
{
if(choose.APPROVE_OPTION==choose.showSaveDialog(this))
{ 
path=choose.getSelectedFile().getPath(); 
newfiles=new File(path);
state.setText(" 我的javaX记事本------"+path);
fname=choose.getSelectedFile().getName();;
try {
newfiles=new File(path);
newfiles.createNewFile();
FileWriter fw=new FileWriter(newfiles); 
fw.write(txt1.getText()); 
fw.close(); 
}catch(IOException e)
{}
}
}

public void notfindmethod() //提示查找不到的方法notfindmethod
{
if(!txt1.getSelectedText().equals(findtxt.getText())) 
{ txt1.setCaretPosition(0); //光标返回文件头部
JOptionPane.showMessageDialog(null,"查找不到对应的字符!","查找错误",JOptionPane.ERROR_MESSAGE);
m26.setEnabled(false); }
}

public void exit()
{ int value;
String[] qq={"返回", "退出"};
value=JOptionPane.showOptionDialog(null, "你确定退出吗?请注意保存文件!", "退出程序?", 
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, qq, qq[0]);
if(value==JOptionPane.YES_OPTION)
return;
else if(value==JOptionPane.NO_OPTION) 
{ System.exit(0); }

}
}//主窗口类结束//

class xxx extends WindowAdapter //关闭窗口XXX类////////////
{myfr fx;
xxx(myfr fxx)
{fx=fxx;}
public void windowClosing(WindowEvent e)
{ JOptionPane.showMessageDialog(null,"欢迎使用本软件!","退出",JOptionPane.INFORMATION_MESSAGE);
fx.dispose();
System.exit(0); 
}
}

class xxxhelp extends WindowAdapter //关闭帮助主题窗口类XXXhelp
{myhelp fx;
xxxhelp(myhelp fxx)
{fx=fxx;}
public void windowClosing(WindowEvent e)
{
fx.dispose();
}
}

class myhelp extends JFrame //帮助主题类/////////////////
{
TextArea helparea;
myhelp()
{
super("高攀帮助主题");
helparea=new TextArea("",45,25,TextArea.SCROLLBARS_NONE);//创建没有滚动条的帮助窗口
helparea.setText(" ");
helparea.append("我的JAVAX记事本是一个用来创建简单的文档的基本的文本编辑器.可用于打开查看TXT文档。");
helparea.append("要编辑文字,请先选定它,然后单击相应菜单上的相关命令。");
helparea.append("鼠标悬停在工具栏上会出现相关提示。本软件在SDK 1.40下编写,");
helparea.append("最好在1.4环境下运行,否则会出现一些小问题,但不影响正常使用……");
setResizable(false);
getContentPane().add(helparea);
show();pack();
helparea.setFont(new Font("宋体",Font.PLAIN,14));
helparea.setEnabled(false);
addWindowListener(new xxxhelp(this));
}
}

class handlemouse extends MouseAdapter //处理右键弹出菜单类

{myfr fx;
handlemouse(myfr fxx)
{fx=fxx;}
public void mouseReleased(MouseEvent n)
{
if(n.isPopupTrigger())
fx.popm.show((Component)n.getSource(),n.getX(),n.getY());
}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -