java编程记事本全部功能都实现.docx

上传人:b****7 文档编号:26682831 上传时间:2023-06-21 格式:DOCX 页数:23 大小:169.32KB
下载 相关 举报
java编程记事本全部功能都实现.docx_第1页
第1页 / 共23页
java编程记事本全部功能都实现.docx_第2页
第2页 / 共23页
java编程记事本全部功能都实现.docx_第3页
第3页 / 共23页
java编程记事本全部功能都实现.docx_第4页
第4页 / 共23页
java编程记事本全部功能都实现.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

java编程记事本全部功能都实现.docx

《java编程记事本全部功能都实现.docx》由会员分享,可在线阅读,更多相关《java编程记事本全部功能都实现.docx(23页珍藏版)》请在冰豆网上搜索。

java编程记事本全部功能都实现.docx

java编程记事本全部功能都实现

两个放到同个包中,一起运行

1.记事本.java

importjavax.swing.text.BadLocationException;

publicclass记事本{

publicstaticvoidmain(Stringargs[]){

Windowwindow1=newWindow();

window1.setTitle("记事本");

window1.setVisible(true);

}

}

2.Window.java

importjavax.swing.*;

importjavax.swing.JOptionPane;

importjava.io.*;

importjava.awt.BorderLayout;

importjava.awt.Color;

importjava.awt.Component;

importjava.awt.Font;

importjava.awt.Frame;

importjava.awt.GraphicsEnvironment;

importjava.awt.event.*;

importjava.awt.*;

importjava.awt.print.*;

importjava.util.*;

importjava.lang.Object;

importjava.lang.Throwable;

importjava.lang.Exception;

importjavax.swing.text.BadLocationException;

importjavax.swing.JFileChooser;

importjavax.swing.event.ListSelectionEvent;

importjavax.swing.event.ListSelectionListener;

importjavax.swing.filechooser.FileNameExtensionFilter;

publicclassWindowextendsJFrameimplementsActionListener{

JMenuBarmenubar;

JMenumenu,menu1,menu2,itemLine,menu3,menu4;

JSplitPanesplitPane;

JMenuItemitemNew,itemOpen,itemSave,itemSaveAs,itemPrint,itemExit;

JMenuItemitemCopy,itemCut,itemPaste,itemDelete;

JMenuItemitemFont,line1,line2,itemhelp,itemabout;

JTextAreatext,text1;

JLabellblStatus;

JTextFieldfield;

KeyHandlerkHandler=newKeyHandler();

JPopupMenupopupMenu;

JToolBarstatusBar;

JCheckBoxMenuItemitemstate;

Window(){

init();

intGUI();

setBounds(500,150,500,500);

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}

voidintGUI(){

text=newJTextArea();

add(newJScrollPane(text),BorderLayout.CENTER);

text.addKeyListener(kHandler);

lblStatus=newJLabel("未修改");

}

voidinit(){//建立一个菜单

menubar=newJMenuBar();//建立文件菜单项

menu=newJMenu("文件(F)");

menubar.add(menu);

setJMenuBar(menubar);

itemNew=newJMenuItem("新建");

itemOpen=newJMenuItem("打开");

itemSave=newJMenuItem("保存");

itemSaveAs=newJMenuItem("另存为");

itemPrint=newJMenuItem("打印");

itemExit=newJMenuItem("退出");

menu.add(itemNew);

menu.add(itemOpen);

menu.add(itemSave);

menu.add(itemSaveAs);

menu.add(itemPrint);

menu.add(itemExit);

itemNew.addActionListener(this);

itemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));

itemOpen.addActionListener(this);

itemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));

itemSave.addActionListener(this);

itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));

itemSaveAs.addActionListener(this);

itemPrint.addActionListener(this);

itemPrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));

itemExit.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

if(lblStatus.getText().equals("已修改")){

intconfirm=JOptionPane.showConfirmDialog(null,"文件已修改,要保存吗?

");

if(confirm==JOptionPane.OK_OPTION)

save();

elseif(confirm==JOptionPane.CANCEL_OPTION)

return;

elseif(confirm==JOptionPane.CLOSED_OPTION)

return;

}

System.exit(0);

}

});

itemExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));

menu1=newJMenu("编辑(E)");//建立编辑菜单项

menubar.add(menu1);

setJMenuBar(menubar);

itemCopy=newJMenuItem("复制");

itemCut=newJMenuItem("剪切");

itemPaste=newJMenuItem("粘贴");

itemDelete=newJMenuItem("删除");

menu1.add(itemCopy);

menu1.add(itemCut);

menu1.add(itemPaste);

menu1.add(itemDelete);

itemCopy.addActionListener(this);

itemCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));

itemCut.addActionListener(this);

itemCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));

itemPaste.addActionListener(this);

itemPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));

itemDelete.addActionListener(this);

itemDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));

menu2=newJMenu("格式(O)");//建立格式菜单项

menubar.add(menu2);

setJMenuBar(menubar);

itemLine=newJMenu("自动换行");//建立二级菜单项

line1=newJMenuItem("选择自动换行");

line2=newJMenuItem("取消自动换行");

itemLine.add(line1);

itemLine.add(line2);

menu2.add(itemLine);

line1.addActionListener(this);

line2.addActionListener(this);

itemFont=newJMenuItem("字体");

menu2.add(itemFont);

itemFont.addActionListener(this);

menu3=newJMenu("查看(V)");//建立查看菜单项

menubar.add(menu3);

setJMenuBar(menubar);

itemstate=newJCheckBoxMenuItem("状态栏");//设置选勾菜单项

menu3.add(itemstate);

itemstate.addActionListener(this);

menu4=newJMenu("帮助(H)");//建立帮助菜单项

menubar.add(menu4);

setJMenuBar(menubar);

itemhelp=newJMenuItem("查看帮助");

menu4.add(itemhelp);

itemhelp.addActionListener(this);

itemabout=newJMenuItem("关于记事本");

menu4.add(itemabout);

itemabout.addActionListener(this);

}

publicvoidactionPerformed(ActionEvente){//选择

if(e.getSource()==itemCopy)

text.copy();

elseif(e.getSource()==itemCut)

text.cut();

elseif(e.getSource()==itemPaste)

text.paste();

elseif(e.getSource()==itemNew)

inew();

elseif(e.getSource()==itemSave)

save();

elseif(e.getSource()==itemOpen)

open();

elseif(e.getSource()==itemSaveAs)

saveas();

elseif(e.getSource()==itemPrint)

print();

elseif(e.getSource()==itemDelete)

delete();

elseif(e.getSource()==line1)

text.setLineWrap(true);//激活自动换行功能

elseif(e.getSource()==line2)

text.setLineWrap(false);

elseif(e.getSource()==itemFont)

font();

elseif(e.getSource()==itemstate)

{

if(itemstate.getState())

state();

else

statusBar.setVisible(false);//状态栏不可见

}

elseif(e.getSource()==itemhelp)

itemhelp();

elseif(e.getSource()==itemabout)

itemabout();

}

voiditemhelp(){

Stringmessage="1.记事本软件界面很简洁,使用方法简单,但是也仅拥有着基本文字编辑的功能。

\n不能对文字进行排版和样式编辑。

"

+"\n2.软件最上方的是标题栏,下面的是菜单栏(点击不同按钮会出现不同的下拉菜单),中间白色空\n白区域为编辑区域,下方的是状态栏,右边是滚动条。

"

+"\n3.下拉菜单说明:

"

+"\n

(1)文件"

+"\n新建:

新建一个空白的文档。

[如果正在编辑中的文档被修改过,会提醒是否保存。

]"

+"\n打开:

打开一个已知的文本文档。

[如果正在编辑中的文档被修改过,会提醒是否保存。

]"

+"\n保存:

保存现在编辑中的文档。

[如果没有保存过会提示保存的位置,如果保存过则会自动覆写上已经保存的文件。

]"

+"\n另存为:

将现在编辑中的文件存到别的地方。

[如果文件保存过则需要保存到别的地方或者改名保\n存到同一位置,如果文件没有保存过则功能同保存。

]"

+"\n打印:

将现在编辑中的文件输出到打印终端进行打印。

"

+"\n退出:

退出记事本软件"

+"\n

(2)编辑"

+"\n剪切:

将选中的文字剪贴到剪贴板中"

+"\n复制:

将选中的文字复制到剪贴板中"

+"\n粘贴:

将剪贴板中的文字粘贴到记事本的光标处"

+"\n删除:

删除选中的文字。

撤销:

撤销上一步操作"

+"\n(3)格式"

+"\n自动换行:

当文字到达最右边的时候,自动切换到下一行"

+"\n字体:

文字的显示设置,可设置字体、字形、大小,并提供预览"

+"\n(4)查看"

+"\n状态栏:

当前光标的所在的行数列数,显示时间以及文字总数,需要重点击状态栏才显示最新消息"

;

JOptionPane.showMessageDialog(this,message,"查看帮助",JOptionPane.PLAIN_MESSAGE);

}

voiditemabout(){

Stringmessage="\n名称:

vivian记事本"+"\n版本:

1.0\n作者:

mengmeng"+"\n\n感谢您的使用\n";

JOptionPane.showMessageDialog(this,message,"关于记事本",JOptionPane.PLAIN_MESSAGE);

}

JLabelstatusLabel1,statusLabel2,statusLabel3;

JMenuBarMenuBar=newJMenuBar();

Containercontainer=getContentPane();

voidstate(){

GregorianCalendartime=newGregorianCalendar();

inthour=time.get(Calendar.HOUR_OF_DAY);

intmin=time.get(Calendar.MINUTE);

intsecond=time.get(Calendar.SECOND);

statusBar=newJToolBar();

intcurLn=1;//设置行数列数

intcurCol=1;

intcaretPos=this.text.getCaretPosition();

intcurSel=0;

try{

//获取指定偏移量处的行号,返回行号的取值范围:

x>=0&&x<文本域总行数

curLn=text.getLineOfOffset(caretPos)+1;

//获取指定行起始处的偏移量,指定行号的取值范围:

x>=0&&x<文本域总行数

curCol+=caretPos-this.text.getLineStartOffset(curLn-1);

}catch(BadLocationExceptionx){

x.printStackTrace();

}

StringstrSel=text.getSelectedText();

if(strSel!

=null){

curSel=strSel.length();

}

/*

*显示状态栏

*/

statusBar.setLayout(newFlowLayout(FlowLayout.LEFT));

statusLabel1=newJLabel("字数总数:

"+this.text.getText().length());

statusLabel2=newJLabel("当前时间:

"+hour+":

"+min+":

"+second);

statusLabel3=newJLabel("当前光标所在行数:

"+curLn+"所在行数:

"+curCol);

statusBar.add(statusLabel1);

statusBar.add(statusLabel2);

statusBar.add(statusLabel3);

container.add(statusBar,BorderLayout.SOUTH);

statusBar.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Clockclock=newClock();

clock.start();}

classClockextendsThread{//模拟的时钟

publicvoidrun(){

while(true){

GregorianCalendartime=newGregorianCalendar();

inthour=time.get(Calendar.HOUR_OF_DAY);

intmin=time.get(Calendar.MINUTE);

intsecond=time.get(Calendar.SECOND);

statusLabel2.setText("当前时间:

"+hour+":

"+min+":

"+second);

try{Thread.sleep(900);}catch(InterruptedExceptionexception){}

}

}

}

voidfont(){//实现字体调整

GraphicsEnvironmentge=GraphicsEnvironment.getLocalGraphicsEnvironment();

JListfontNames=newJList(ge.getAvailableFontFamilyNames());

intresponse=JOptionPane.showConfirmDialog(null,newJScrollPane(fontNames));

ObjectselectedFont=fontNames.getSelectedValue();

if(response==JOptionPane.YES_OPTION&&selectedFont!

=null)

System.out.println("选择了"+selectedFont);

else

System.out.println("取消了或没作出选择");

}

voiddelete(){//实现删除功能

text.cut();

}

voidprint(){//实现打印功能

StringprintStr=text.getText().trim();//获取需要打印的目标文本

if(printStr!

=null&&printStr.length()>0)//当打印内容不为空时

{

//PAGES=getPagesCount(printStr);//获取打印总页数

PrinterJobmyPrtJob=PrinterJob.getPrinterJob();//获取默认打印作业

PageFormatpageFormat=myPrtJob.defaultPage();//获取默认打印页面格式

//myPrtJob.setPrintable(this,pageFormat);//设置打印工作

if(myPrtJob.printDialog())//显示打印对话框

{

try

{

myPrtJob.print();//进行每一页的具体打印操作

}

catch(PrinterExceptionpe)

{

pe.printStackTrace();

}

}

}

else

{

//如果打印内容为空时,提示用户打印将取

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 农学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1