java实验文本编辑器_第1页
java实验文本编辑器_第2页
java实验文本编辑器_第3页
java实验文本编辑器_第4页
java实验文本编辑器_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

课程名称:iava试验

姓名:

学号:

一.开发环境描述及其相关配置描述

J2SDK1.6或以上版木,程序编辑环境EditPlus,UltraEdit,或其他Java集成开发环

二.程序的模块功能描述和涉及到的关键JavaAPI列表

模块:记事本:

一)、菜单

1、文件:新建,打开,保存,另存为,退出

2、编辑:剪切,复制,粘贴,替换,全部替换,插入文本,删除

3、格式:自动投行,颜色,字体

4、查找

5、帮助

二)、界面

1、编辑区

三、程序源代码(关键部分附带注释)

importjava.awt.*;

importjava.awt.event.*;

importjava.awt.datatransfer.*;

importjavax.swing.*;

importjava.io.*;

importjava.lang.*;

publicclassNotepadextendsJFrcime{

privatefinalColorcolorvaluesf]=

{Color,black,Color.blue,Color.red,Color,green};〃定义颜

色数组

StringstyleNamesf]={"Bold","Italic"};//定义风格数组

StringfontNames[]={〃宋体〃,〃华文行楷〃,〃隶书〃};〃字体数组

String[]sizeString=newString[30];〃字号数组

int[]size=newint[30];〃及字号数组对应的字号整数,用于设置文字大

privateJRadioButtonMenuItemcolorltems[],fonts[];

privateJChcckBoxMenuItemstyleltcms[];

privateJTextAreadisplayText;〃定义文本编辑区

privateButtonGroupfon(Group,colorGroup;〃字体组,跟字色组

privateintstyle;//字体风格

privateJScrollPanescroll;〃为文木编辑区供应滚动条

privateStringselectText=存放文本编辑区中选中的文本内容

privateintrowNumber=0;

privatefd=new(this);

//setupGUI

publicNotepad0

super(〃记事本〃);〃标题

〃创建菜单条

JMenuBarbar=newJMenuBcirO;

setJMcnuBar(bar);

//设置文件菜单及其菜单项

JMcnu=newJMenu("文件(F)”);

('F');

//设置新建菜单项

JMenuItemnewItem=newJMcnuItcm(〃新建(N)”);

newltem.setMnemonic('N');

(newltem);

newltem.addActionListoner(

newActionListener0{

publicvoidactionPerformed(ActionEventevent)

{displayText.setText("");

}});

//设置打开菜单项

JMenuItemopenitem=newJMenuItem(“打开(0)”);

openitem.setMnemonic('O');

(openltem);

openItem.addActionListoner(

newActionListener0(

publicvoidactionPerformed(ActionEventevent)

(

fd.setTitle(〃打开〃);//设置标题

fd.show();

if(fd.getFile()!=null)I

=new());〃用从fd中取得的文件建立新文件,

即打开的文件

displayText.sctText(;

try(

f=new(file);

BufferedReaderb=newBufferedRcadcr(f);//

按行读打开的文件,然后传入文本域

Strings;

try{

while((s=b.readLineO)!=null){

displayText.append(s+"\n");〃将给定

文本追加到文本域的当前文本(即把读的内容加入文本域)

f.close();

fd.setTitle(〃另存为〃);

fd.show();

if(fd.getFile()!=null){

=new());

try(

PrintWriterpw=newPrintWriter(file);

pw.print(displayText.getText0);

〃这一部分用于把textAreal中的内容打印到pw指

定的文件file中去,file围在对话框中选定的文件

pw.flush();〃刷新

该流的缓冲,然后关闭该流

pw.closeO;

}catch(tionex){ex.printStackTraceO;}

)

else(return;}

}});

0;〃加分隔线

//设置退出菜单项

JMenuItemexitItem=newJMenuItem("退出(X)”);

exitltem.setMnemonic('x');

(exititem);

exititem.addActionListener(

newActionListener(){

publicvoidactionPerformcd(ActionEvcntevent)

(

System,exit(0);

}

)

);

bar.add();

〃创建编辑菜单及其菜单项

JMcnueditMenu=newJMonu(〃编辑(E)〃);

editMenu.setMnemonic(,E');

〃剪切菜单选项

JMcnuItcmcutItem=newJ"cnultcni(〃剪切(T)”);

cutitem.setMnemonic('T');

editMenu.cidd(cutitem);

cutItern.addActionListener(

newActionListener0{

publicvoidactionPerformed(ActionEventevent){

selectTcxt=displayText.getSelectedTextO;〃取得

选定的文本

intstart=displayText.getSelectionStart();//

选定的文本的起先位置

intend=displayText.getSelectionEndO;//i^

定的文本的结束位置

displayText.replaceRange(,,z,,start,end);/*用

指定替换文本替换指定起先位置及结束位置之间的文本。

这里指定替换文

本为空,即为剪切了文本*/

)

)

);

〃复制菜单选项

JMcnuItemcopyitem=newJMenuItem("复制(C)”);

copyitem.setMnemonic('C');

editMenu.add(copyitem);

copyItcm.addActionListener(

newActionListener(){

publicvoidactionPerformed(?\ctionEventevent){

selectText=displayText.getSelectcdText();〃获得选中的内容,

并保存在selectText里

)

)

);

〃粘贴的实现

JMcnuItempastcltcm=newJNonuIt。In(“粘贴(P)”);

pasteitem.setMnemonic('P');

editMenu.add(pasteitem);

pasteltem.addActionListener(

newActionListener(){

publicvoidactionPerformed(ActionEventevent){

intposition=displayText.getCaretPositionO;〃获得鼠标当前

位置

displayText.insert(selectText,position);〃插入到鼠标当前位

}

}

);

editMenu.addSeparator();〃加分隔线

〃替换的实现

JMonuTtoms=newJMcnuItem("替换(R))”):

sLR,);

editMenu.add(s);

s(

newActionListener0{

publicvoidactionPerformed(ActionEventevent){

JPanelsJPanel0;

JLabellookupLabel=newJLabel(“要替换的内容

〃);

JTextFieldinputText=newJTextField(lO);

JLabelsJLabel("替换为:”);

JTextFieldchangetoText=n3wJTextField(10);

s(lookupLabel);

s(inputText);

s(s);

s(changetoText);

JOptionPane.showMessageDialog(nul1,s);

Stringtext=displayText.getTextO;〃获得整个文本内

String

changeText=tcxt.replaceFirst(inputText.getText(),changetoText.getText

());〃获得替换后的内容

displayText.setText(chcingeText);

)

)

);

〃全部替换的实现

JMenuItemas=newJMenuItem("全部替换(Q))〃);

as('Q');

cditMcnu.add(as);

as(

newActionListener(){

publicvoidactionPerformed(ActionEventevent){

JPanelsJPanel0;

JLabellookupLabel=newJLabel(〃要替换的内

容〃);

JTextFieldinputText:newJTextField(lO);

JLcibelsJLabel("替换为:");

JTextFieldchangetoTc>:t=ncwJTextFicld(lO);

s(lookupLabel);

s(inputText);

s(s);

s(changetoText);

JOptionPane.showMessageDialog(null,s);

Stringtext=displayText.getText():〃获得整个文

本内容

String

changcTcxt=tcxt.replaccAl1(inputText.getText(),changetoText.getText()

);〃获得替换后的内容

displayText.setText(changeText);

}

}

);

editMenu.addSeparator();〃加分隔线

〃插入子菜单

JMenuIteminsertItem=newJMenuItem("插入文本(K)”);

insertitem.setMnemonic(,K');

editMenu.add(insertitem);

insertitem.addActionListencr(

newActionListenerO{

publicvoidactionPerformed(ActionEventevent){

JPanelinsertPanel=ncwJPancl();

JLabelinsertLabel=newJLabel("要插入的内容");

JTextFieldinputText=newJTextFie1d(10);

insertPanel.add(insertLabel);

insertPanel.add(inputText);

JOptionPane.showMessageDialog(null,insertPanel);

intfromTndex=displayText.gctCarctPosition();〃取得当前

的光标位置

displayText.insert(inputText.getText(),fromlndex);

)

}

);

〃删除子菜单

JMenuItemRemoveItem=newJMenuItem("删除(G)”);

Removeitem.selMnemonic('G');

cditMonu.add(RemoveItem);

Removeitem.addActionListener(newActionListsner()

{

publicvoidactionPerformed(ActionEvente)

{intstart=displayText.getSelectionStart0;

intend=displayText.getSelectionEnd();

displayTcxt.replaccRangc(nul1,start,end);

)

));

editMenu.addSeparator();

bar.add(editMenu);//addeditMenu

//创建格式菜单及其菜单项

JMenuformatMenu=newJMenu("格式(R)”);

formatMenu.setMnemonic('R');

〃自动换行的功能切换

JMenuItemchangeitem=newJMenuItem("自动换行(W)”);

changeitem.setMnemonic('W');

formatMenu.add(changeltem);

changeitem.addActionListener(

newActionListener(){

booleanvar=false;

publicvoidactionPerformed(ActionEventevent){

if(var)var=false;

elsevar=true;

displayText.setLineWrcip(var);

)

(

);

//颜色菜单项

Stringcolors[]={〃黑色","蓝色〃,〃红色",〃绿色〃};

JMenucolorMenu=newJMenu("颜色(C));

colorMenu.setMnemonic('C');

colorltems=newJRadioButtonMenuItemEcolors,length];

colorGroup=newButtonGroupO;

ItcmHandleritemHandler=newItemHandler();

//创建颜色按钮监听器

for(intcount=0;count<colors.length;count।।)(

colorltems[count]=

newJRadioButtonMenuItem(colors[count]);

colorMenu.add(colorltems[count]);

colorGroup.add(colorltems[count]);

colorltems[count].addActionListener(itemHandler);

)

//默认颜色

colorltems[0].setSelected(true);

//将颜色菜单项菜单加入菜单

formatMenu.add(colorMenu);

formatMenu.addSeparator();〃加分隔线

//字体菜单项

JMenufontMcnu=nowJMenu("字体(n)〃);

fontMenu.setMnemonic('n');

fonts=newJRadioBu11onMenu11em[fontNames.length];

fontGroup=newButtonGroupO;

//创建字体按钮监听器

for(intcount=0;count<fonts,length;count++){

fonts[count]=nowJRadioButtonMenuTtom(fontNames[count]):

fontMenu.add(fonts[count]);

fontGroup.add(fonts[count]);

fonts[count.addActionListener(itemHandler);

(

//默认字体

fonts[0].setSelected(true);

fontMenu.addSeparator();

//设置风格菜单

styleltems=newJCheckBoxMenuItera[styleNames.length];

for(intcount=0;count<styleNames.length;count++)

stylcltems[count]=

newJCheckBoxMenuItern(styleNames[count]);

fontMenu.add(styleltems[count]);

StylcHandlcrstylcHandler=newStyleHandler();

stvleltemsEcount].addItemListener(styleHandler);

}

formatMcnu.add(fontMenu);

bar.add(formatMenu);

〃创建查找菜单

JMenusearchMenu=newJMenu(〃查找(S)”);

secirchMenu.setUnemonic('H');

bar.add(searchMenu);//添加

〃创建帮助菜单

JMenuhelpMenu=newJMenu("帮助(H)”);

helpMenu.setMnemonic('H');

//设置“关于(A)...”菜单项

JMenuItemaboutItem=newJMenuItern("关于(A)...");

aboutltem.setMnemonic('A');

helpMenu.add(aboutltem);

aboutltem.addActionListener(

newActionListener(){

publicvoidactionPerformed(ActionEv^ntevent)

{JOptionPane.showMessageDialog(Notepad.this,

〃文本编辑器”,

〃关于",JOptionPane.PLAIN_MESSAGE);

rowNumber=displayText.getRowsO;

JOptionPane.showMessageDialog(nul1,z,,,+rowNumber);

)

)

);

〃添加屈助菜单项

JMenuItemhelpitem=newJMenuItem("帮助主题(H)...");

helpitem.setMnemonic('H');

helpMenu.add(helpltem);

helpitem.addActionListener(

newActionListener(){

publicvoidactionPerformed(ActionEventevent){

JTextAreahelpText=newJTextArea(

“格式里的自动换行说明:单击一次切换功能\n只能实现整个

文本的风格,字体等的设置\n〃+

〃复制,粘贴可以实现插入到鼠标当前位置\n标题栏显示行数

〃);

JScrollPanescroller=newJScrollPane(helpText);

JOptionPane.showMesscigeDialog(null,scroller);

}

}

);

bar.add(helpMenu);//添加

〃设置字体选项面板

〃初始化字体大小数组

for(inti=0;i<size.length;i++){

sizeStringEi]:〃〃+(i+12)*2;

size[i]=(i+12)*2;

)

Containercontainer=getContentPaneO;

container.setLayout(newBorderLayout());

//设置面板显示文本

displayText=newJTextArea();

displayTcxt.sctForcground(colorvalues[0]);

displayText.setFont(newFont("Serif",Font.PLAIN,24));〃设

置默认字体

scrol1=new

JScrollPane(displayText,JScrollPane.VERTICALSCROLLBARAS.NEEDED,

JScrollPane.HORIZONTAL_SCROLLBAR^ALWAYS);

container.add(scroll,BorderLayout.CENTER);

displayText.addKeyListener(〃通过对displayText注册键盘事务来

获得当前文本的行数

newKeyListener(){

publicvoidkeyPressed(KeyEventevent)(

rowNumber=displayText.getLineCount();〃获得文本区的函数

setTitle(〃总共“+rowNumber+"行”);//设置标题

}

publicvoidkeyReleasedCKeyEventevent){〃空

)

publicvoidkeyTyped(KeyEventevent){〃空

}

}

);

setSize(700,500);

setVisible(true);

}

〃主函数

publicstaticvoidmain(Stringargs[])

{

Notepadapplication=newNotepad();

application.setDefaultCloseOperation(JFrame.EXITONCLOSE);

)

〃字体和颜色监听器

privateclassItemHandlerimplementsActionListencr(

publicvoidactionPerformed(ActionEventevent)

(

//颜色

for(in

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论