java编程记事本全部功能都实现.docx
- 文档编号:27496598
- 上传时间:2023-07-02
- 格式:DOCX
- 页数:21
- 大小:222.01KB
java编程记事本全部功能都实现.docx
《java编程记事本全部功能都实现.docx》由会员分享,可在线阅读,更多相关《java编程记事本全部功能都实现.docx(21页珍藏版)》请在冰豆网上搜索。
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{
JMenuBar
menubar;
JMenumenu,menu1,menu2,itemLine,menu3,menu4;
JSplitPane
JMenuItem
splitPane;
itemNew,itemOpen,itemSave,itemSaveAs,itemPrint,itemExit
JMenuItem
itemCopy,itemCut,itemPaste,itemDelete;
JMenuItem
JTextArea
itemFont,line1,line2,itemhelp,itemabout;text,text1;
JLabellblStatus
JTextField
field
KeyHandlerkHandler=newKeyHandler();
JPopupMenupopupMenu;
JToolBarstatusBar;
JCheckBoxMenultemitemstate;
Window(){
init();
intGUI();
setBounds(500,150,500,500);
DISPOSE_ON_CLOSE);
setDefaultCloseOperation(JFrame.
}
voidintGUI(){
void
init(){
menubar
//建立一个菜单
=newJMenuBar();//建立文件菜单项
menu=newJMenu("文件(F)");
menubar.add(menu);
setJMenuBar(menubar);
itemNew=newJMenultem("新建”);
itemOpen=newJMenultem("打开”);
itemSave=newJMenultem("保存”);
itemSaveAs=newJMenultem("另存为");itemPrint=newJMenultem("打印”);
itemExit=newJMenultem("退岀”);
menu.add(itemNew);
menu.add(itemOpen);
menu.add(itemSave);menu.add(itemSaveAs);
menu.add(itemPrint);
menu.add(itemExit);
itemNew.addActionListener(this);
itemNew.setAccelerator(KeyStroke.
Event.CTRL_MASK));
itemOpen.addActionListener(itemOpen.setAccelerator(KeyStroke.
tEvent.CTRL_MASK));
itemSave.addActionListener(
getKeyStroke(KeyEvent.
this);
getKeyStroke(KeyEvent
VK_N,Input
VK_O,Inpu
itemSave.setAccelerator(KeyStroke.tEvent.CTRL_MASK));
itemSaveAs.addActionListener(
itemPrint.addActionListener(
getKeyStroke(KeyEvent.VK_S,Inpu
this);
this);
itemPrint.setAccelerator(KeyStroke.utEvent.CTRL_MASK));
itemExit.addActionListener(
getKeyStroke(KeyEvent.VK_P,Inp
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(
itemCopy.setAccelerator(KeyStroke.tEvent.CTRL_MASK));
itemCut.addActionListener(
粘贴");
"删除");
this);
getKeyStroke(KeyEvent.VK_C,Inpu
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);
menu4=newJMenu("帮助(H)");//建立帮助菜单项menubar.add(menu4);
setJMenuBar(menubar);
itemhelp=newJMenuItem("查看帮助");
menu4.add(itemhelp);
itemhelp.addActionListener(this);
itemabout=newJMenuItem("关于记事本");menu4.add(itemabout);
//选择
publicvoidactionPerformed(ActionEvente){if(e.getSource()==itemCopy)
text
.copy();
else
if
(e.getSource()==
itemCut)
text
.cut();
else
if
(e.getSource()==
itemPaste
text
.paste();
else
if
(e.getSource()==
itemNew)
inew();
else
if
(e.getSource()==
itemSave
)
save();
else
if
(e.getSource()==
itemOpen
)
open();
else
if(e.getSource()==
itemSaveAs
)
saveas();
else
if(e.getSource()==
itemPrint
)
print();
else
if(e.getSource()==
itemDelete
)
delete();
else
if
(e.getSource()==
line1
)
text
.setLineWrap(
true);
//激活自动换行功能
else
if
(e.getSource()==
line2
)
text
.setLineWrap(
false);
else
if
(e.getSource()==
itemFont)
font();
else
if
(e.getSource()==
itemstate)
{
if(itemstate.getState())state();
else
statusBar.setVisible(}
elseif(e.getSource()==itemhelp();
elseif(e.getSource()==
false);//状态栏不可见
itemhelp)
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();
}
/*
*显示状态栏
*/
this.text.getText().length());
数:
"+curCol);
EXIT_ON_CLOSE);
this.setDefaultCloseOperation(JFrame.
Clockclock=
newClock();
clock.start();}
classClock
extendsThread{
//模拟的时钟
public
voidrun(){
while(true){
GregorianCalendartime=newGregorianCalendar();
voidfont(){//实现字体调整
null)
System.
else
System.
}
out.println("选择了"+selectedFont);
out.println("取消了或没作出选择");
voiddelete(){//实现删除功能text.cut();
try
}
//进行每一页的具体打印操作
myPrtJob.print();
}catch(PrinterExceptionpe)
{
pe.printStackTrace();
}
}
}
else
{
//如果打印内容为空时,提示用户打印将取消JOptionPane.showConfirmDialog
(null,"对不起,文本为空,打印取消!
","Empty",
JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);}
}
voidsave(){
//实现保存功能
PrintWriterw=
null;
FileNameExtensionFilterfilter=
new
FileNameExtensionFilter("*.txt","txt");
JFileChooserChooser=newJFileChooser();
Chooser.setFileFilter(filter);
if(lblStatus.getText().equals("已修改")){
Chooser.setSelectedFile(newFile(getName()));
intresult=Chooser.showSaveDialog(null);
if(result==JFileChooser.APPROVE_OPTION){
Filefile=Chooser.getSelectedFile();
System.out.println(file.getPath());
if(!
file.getName().endsWith(".txt")){
file=newFile(file.getPath()+".txt");
System.out.println(file.getPath());
}
try{
w=newPrintWriter(newBufferedWriter(newFileWriter(file)));
Stringss[]=text.getText().split("\n");for(Stringst:
ss)
w.println(st);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
w.close();
}
}
}
}
voidopen(){//实现打开功能
inew();
FileNameExtensionFilterfilter=new
FileNameExtensionFilter("*.txt","txt");JFileChooserchooser=newJFileChooser();chooser.setFileFilter(filter);
ints=chooser.showOpenDialog(null);
Stringstr="";
if(s==JFileChooser.APPROVE_OPTION){
Filefile=chooser.getSelectedFile();
BufferedReaderr=null;file.getName().endsWith(".txt");
try{
r=newBufferedReader(newFileReader(file));
while(true){
str=r.readLine();
if(str==null)
break;
text.append(str+"\n");
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{r.close();}
catch(IOExceptione){e.printStackTrace();}
}
Window.this.setTitle(file.getName());
}
}
voidsaveas(){//实现另存为功能
FileNameExtensionFilterfilter=
newFileNameExtensionFilter(
"*.txt"
PrintWriterw=null;
"txt");
JFileChooserChooser=newJFileCho
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 编程 记事本 全部 功能 实现