记事本源代码java编写.docx
《记事本源代码java编写.docx》由会员分享,可在线阅读,更多相关《记事本源代码java编写.docx(17页珍藏版)》请在冰点文库上搜索。
![记事本源代码java编写.docx](https://file1.bingdoc.com/fileroot1/2023-5/5/fcfe86f8-bf11-4af5-bfc6-814c3e29381a/fcfe86f8-bf11-4af5-bfc6-814c3e29381a1.gif)
记事本源代码java编写
package记事本;
importjava.awt.*;
importjava.io.*;
importjava.awt.Font;
importjava.awt.event.*;
importjava.awt.datatransfer.*;
importjavax.swing.JColorChooser;
importjavax.swing.JFrame;
importjavax.swing.JOptionPane;
//importnotepad.MenuList;
import记事本.FontDialog;
classWindowTextextendsFrameimplementsActionListener
{staticDimensionscreen=Toolkit.getDefaultToolkit().getScreenSize();
TextAreatextarea;
//publicMenuListmenu;
MenuBarmenubar;
FontDialogfont;
JFramenotepad;
Menumenu1,menu2,menu3,menu4;
MenuItemxinjian,save,open,quanxuan,exit,copy,cut,paste,delete,huanhang,ziti,yanse,about;
FileDialogfiledialog_save,
filedialog_load;
BufferedReaderin;
FileReaderfile_reader;
BufferedWriterout;
FileWritertofile;
Clipboardclipboard=null;
inttext_event=0;
privateObjectfontChooser1;
WindowText(Strings)
{
super(s);
notepad=newJFrame("记事本");
clipboard=getToolkit().getSystemClipboard();
font=newFontDialog(notepad,screen.width/2-240,screen.height/2-150);
menubar=newMenuBar();
menu1=newMenu("文件(F)");
menu2=newMenu("编辑(E)");
menu3=newMenu("格式(O)");
menu4=newMenu("关于(A)");
xinjian=newMenuItem("新建");
xinjian.setShortcut(newMenuShortcut(KeyEvent.VK_N));
save=newMenuItem("保存");
save.setShortcut(newMenuShortcut(KeyEvent.VK_S));
open=newMenuItem("打开");
open.setShortcut(newMenuShortcut(KeyEvent.VK_O));
quanxuan=newMenuItem("全选");
quanxuan.setShortcut(newMenuShortcut(KeyEvent.VK_A));
exit=newMenuItem("退出");
exit.setShortcut(newMenuShortcut(KeyEvent.VK_E));
copy=newMenuItem("复制");
copy.setShortcut(newMenuShortcut(KeyEvent.VK_C));
cut=newMenuItem("剪切");
cut.setShortcut(newMenuShortcut(KeyEvent.VK_X));
paste=newMenuItem("粘贴");
paste.setShortcut(newMenuShortcut(KeyEvent.VK_V));
delete=newMenuItem("删除");
delete.setShortcut(newMenuShortcut(KeyEvent.VK_D));
huanhang=newCheckboxMenuItem("自动换行");
ziti=newMenuItem("字体");
yanse=newMenuItem("颜色");
about=newMenuItem("关于记事本");
textarea=newTextArea();
textarea.setFont(newFont("宋体",Font.PLAIN,16));
filedialog_save=newFileDialog(this,"保存文件对话框",FileDialog.SAVE);
filedialog_save.setVisible(false);
filedialog_load=newFileDialog(this,"打开文件对话框",FileDialog.LOAD);
filedialog_load.setVisible(false);
filedialog_save.addWindowListener(newWindowAdapter()//对话框增加适配器
{
publicvoidwindowClosing(WindowEvente)
{
filedialog_save.setVisible(false);
}
}
);
filedialog_load.addWindowListener(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
filedialog_load.setVisible(false);
}
}
);
addWindowListener(newWindowAdapter()//增加窗口适配器
{
publicvoidwindowClosing(WindowEvente)
{
if(text_event==1)
{
Stringm="内容被修改,是否保存";
intok=JOptionPane.showConfirmDialog(null,m,"确认对话框",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
filedialog_save.setVisible(true);
Stringname=null;
name=filedialog_save.getFile();
if(name!
=null)
{
System.exit(0);
}
}
elseif(ok==JOptionPane.NO_OPTION)
{
System.exit(0);
}
}
else
{
System.exit(0);
}
}
}
);
menubar.add(menu1);
menu1.add(xinjian);
menu1.add(save);
menu1.add(open);
menu1.add(quanxuan);
menu1.addSeparator();
menu1.add(exit);
menubar.add(menu2);
menu2.add(copy);
menu2.add(cut);
menu2.add(paste);
menu2.addSeparator();
menu2.add(delete);
menubar.add(menu3);
menu3.add(huanhang);
menu3.add(ziti);
menu3.add(yanse);
menubar.add(menu4);
menu4.add(about);
textarea.setFont(font.getFont());
menu1.addActionListener(this);
xinjian.addActionListener(this);
save.addActionListener(this);
open.addActionListener(this);
quanxuan.addActionListener(this);
exit.addActionListener(this);
menu2.addActionListener(this);
copy.addActionListener(this);
cut.addActionListener(this);
paste.addActionListener(this);
delete.addActionListener(this);
menu3.addActionListener(this);
huanhang.addActionListener(this);
ziti.addActionListener(this);
yanse.addActionListener(this);
//textarea.menu.Font.addActionListener(this);
yanse.addActionListener(this);
about.addActionListener(this);
setBounds(100,100,500,500);
setMenuBar(menubar);
add(textarea,BorderLayout.CENTER);
setVisible(true);
validate();
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==xinjian)//新建一个文本框
{
if(text_event==1)
{
Stringm="内容被修改,是否保存";
intok=JOptionPane.showConfirmDialog(this,m,"确认对话框",
JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
filedialog_save.setVisible(true);
Stringname=null;
name=filedialog_save.getFile();
if(name!
=null)
{
text_event=0;
textarea.setText(null);
}
}
elseif(ok==JOptionPane.NO_OPTION)
{
textarea.setText(null);
}
}
else
{
textarea.setText(null);
}
}
elseif(e.getSource()==save)//保存文件
{
filedialog_save.setVisible(true);
if(filedialog_save.getFile()!
=null)
{
try
{Filefile=newFile(filedialog_save.getDirectory(),filedialog_save.getFile());
tofile=newFileWriter(file);
out=newBufferedWriter(tofile);
out.write(textarea.getText(),0,(textarea.getText()).length());
out.close();
tofile.close();
}
catch(IOExceptione2){}
}
else
{
textarea.setText("没有保存文件");
}
}
elseif(e.getSource()==open)//打开文件
{
filedialog_load.setVisible(true);
textarea.setText(null);
Strings;
if(filedialog_load.getFile()!
=null)
{try
{
Filefile=newFile(filedialog_load.getDirectory(),filedialog_load.getFile());
file_reader=newFileReader(file);
in=newBufferedReader(file_reader);
while((s=in.readLine())!
=null)
textarea.append(s+'\n');
in.close();
file_reader.close();
}
catch(IOExceptione2){}
}
else
{
textarea.setText("没有打开文件");
}
}
elseif(e.getSource()==exit)//退出窗口
{
System.exit(0);
}
elseif(e.getSource()==copy)//复制
{
Stringtemp=textarea.getSelectedText();
StringSelectiontext=newStringSelection(temp);
clipboard.getContents(null);
textarea.append(temp);
}
elseif(e.getSource()==cut)//剪切
{
Stringtemp=textarea.getSelectedText();
StringSelectiontext=newStringSelection(temp);
clipboard.setContents(text,null);
intstart=textarea.getSelectionStart();
intend=textarea.getSelectionEnd();
textarea.replaceRange("",start,end);
}
elseif(e.getSource()==paste)//粘贴
{
Transferablecontents=clipboard.getContents(this);
DataFlavorflavor=DataFlavor.stringFlavor;
if(contents.isDataFlavorSupported(flavor))
try{
Stringstr;
str=(String)contents.getTransferData(flavor);
textarea.append(str);
}
catch(Exceptionee){}
}
elseif(e.getSource()==quanxuan)//全选
{
textarea.selectAll();
}
elseif(e.getSource()==delete)//删除
{
textarea.setText(null);
}
elseif(e.getSource()==ziti)//改变字体
{
font.Dialog.setVisible(true);
if(textarea.getFont()!
=font.getFont())
textarea.setFont(font.getFont());
}
elseif(e.getSource()==yanse)//设置字体颜色
{
yanse.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEventevent){
Componentcontainer=null;
Colorcolor=JColorChooser.showDialog(container,"字体颜色选择",Color.BLACK);
textarea.setForeground(color);}
}
);
}
elseif(e.getSource()==about)//关于记事本
{
JOptionPane.showMessageDialog(this,"名称:
一个简单的记事本\n编写人:
花花\n运行平台:
Windows平台\n"+
"编写时间:
2011年11月11日","关于记事本",JOptionPane.INFORMATION_MESSAGE);
}
}
publicvoiditemStateChanged(ItemEvente){
//TODOAuto-generatedmethodstub
}
}
publicclass记事本
{
publicstaticvoidmain(String[]args)
{
newWindowText("一个简单的记事本");
}
}
package记事本;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.JButton;
importjavax.swing.JCheckBox;
importjavax.swing.JDialog;
importjavax.swing.JFrame;
importjavax.swing.JTextArea;
publicclassFontDialogimplementsItemListener,ActionListener,WindowListener{
publicJDialogDialog;
publicJCheckBoxBold,Italic;
publicListSize,Name;
publicintFontName;
publicintFontStyle;
publicintFontSize;
publicJButtonOK,Cancel;
publicJTextAreaText;
publicFontDialog(JFramenotepad,intx,inty){
GraphicsEnvironmentg=GraphicsEnvironment.getLocalGraphicsEnvironment();
Stringname[]=g.getAvailableFontFamilyNames();
Bold=newJCheckBox("Bold",false);
Italic=newJCheckBox("Italic",false);
Dialog=newJDialog(notepad,"字体选择框",true);
Text=newJTextArea("字体预览用例\n9876543210\nAaBbCcXxYyZz");
OK=newJButton("确定");
Cancel=newJButton("取消");
Size=newList();
Name=newList();
inti=0;
Name.add("DefaultValue");
for(i=0;ifor(i=8;i<257;i++)Size.add(String.valueOf(i));
FontName=0;
FontStyle=0;
FontSize=8;
Dialog.setLayout(null);
Dialog.setLocation(x,y);
Dialog.setSize(480,306);
Dialog.setResizable(false);
OK.setFocusable(false);
Cancel.setFocusable(false);
Bold.setFocusable(false);
Italic.setFocusable(false);
Name.setFocusable(false);
Size.setFocusable(false);
Name.setBounds(10,10,212,259);
Dialog.add(Name);
Bold.setBounds(314,10,64,22);
Dialog.add(Bold);
Italic.setBounds(388,10,64,22);
Dialog.add(Italic);
Size.setBounds(232,10,64,259);
Dialog.add(Size);
Text.setBounds(306,40,157,157);
Dialog.add(Text);
OK.setBounds(3