📄 notepad.java
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;//包含事件包
import java.util.*;
import java.util.Date;//包含时间、日期、数字
import java.text.SimpleDateFormat;
import javax.swing.*;
import javax.swing.event.*;
//文件MainWindow.java
class MainWindow extends Frame implements ActionListener,DocumentListener,Runnable
{
MenuBar menubar;
Menu menuFile,menuEdit,menuFormat,menuHelp;
MenuItem itemNew,itemLoad,itemSave,itemSaveAs,itemExit,itemCut,itemCopy,itemPaste,itemRemove,itemSelectAll,itemTime,itemLineWrap,itemFont,itemHelp,itemAbout;
FileDialog filedialog_Save,filedialog_Load;
JTextArea text;
JScrollPane scroll;
Date nowTime;
SimpleDateFormat time;
BufferedReader in;
FileReader file_reader;
BufferedWriter out;
FileWriter tofile;
int text_event = 0,clickTimes =1;
//---------------主窗口的定义-------------------------
MainWindow(String s)
{
setTitle(s);
Toolkit tool=getToolkit();
Dimension dim =tool.getScreenSize();
setBounds(40,40,dim.width/2,dim.height/2);//窗口大小
text= new JTextArea();//文本区
scroll = new JScrollPane(text);//滚动面板
//创建菜单项
menubar =new MenuBar();
menuFile =new Menu("文件");
menuEdit =new Menu("编辑");
menuFormat =new Menu("格式");
menuHelp =new Menu("帮助");
//创建菜单条目
itemNew =new MenuItem("新建");
itemLoad =new MenuItem("打开");
itemSave =new MenuItem("保存");
itemSaveAs =new MenuItem("另存为");
itemExit=new MenuItem("退出");
itemCut=new MenuItem("剪切");
itemCopy=new MenuItem("复制");
itemPaste=new MenuItem("粘贴");
itemRemove=new MenuItem("删除");
itemSelectAll=new MenuItem("全选");
itemTime=new MenuItem("时间");
itemLineWrap=new MenuItem("自动换行");
itemFont=new MenuItem("字体");
itemHelp=new MenuItem("帮助主题");
itemAbout=new MenuItem("关于");
//打开的文本框的定义
filedialog_Save =new FileDialog(this,"保存文件",FileDialog.SAVE);
filedialog_Load =new FileDialog(this,"打开文件",FileDialog.LOAD);
//向窗体内加入菜单条目
menubar.add(menuFile);
menubar.add(menuEdit);
menubar.add(menuFormat);
menubar.add(menuHelp);
menuFile.add(itemNew);
menuFile.add(itemLoad);
menuFile.add(itemSave);
menuFile.add(itemSaveAs);
menuFile.addSeparator();
menuFile.add(itemExit);
menuEdit.add(itemCut);
menuEdit.add(itemCopy);
menuEdit.add(itemPaste);
menuEdit.add(itemRemove);
menuEdit.addSeparator();
menuEdit.add(itemSelectAll);
menuEdit.add(itemTime);
menuFormat.add(itemLineWrap);
menuFormat.add(itemFont);
menuHelp.add(itemHelp);
menuHelp.add(itemAbout);
add(scroll);
//菜单条目的事件反映
itemNew.addActionListener(this);
itemLoad.addActionListener(this);
itemSave.addActionListener(this);
itemSaveAs.addActionListener(this);
itemExit.addActionListener(this);
itemCut.addActionListener(this);
itemCopy.addActionListener(this);
itemPaste.addActionListener(this);
itemRemove.addActionListener(this);
itemSelectAll.addActionListener(this);
itemTime.addActionListener(this);
itemFont.addActionListener(this);
itemLineWrap.addActionListener(this);
itemAbout.addActionListener(this);
//添加适配器,实行内部监程
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
(text.getDocument()).addDocumentListener(this);
filedialog_Save.setVisible(false);//保存视窗隐藏
filedialog_Load.setVisible(false);//打开视窗隐藏
setMenuBar(menubar);
setVisible(true);//主视窗隐藏
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==itemNew)
{
if( text_event == 1)
{
String message = "文本内容被修改,是否保存?";
int ok = JOptionPane.showConfirmDialog(null,message,"确认对话框",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
filedialog_Save.setVisible(true);
if(filedialog_Save.getFile()!=null)
{
try{
File file =new File(filedialog_Save.getDirectory(),filedialog_Save.getFile());
tofile=new FileWriter(file);
out =new BufferedWriter(tofile);
out.write(text.getText(),0,(text.getText()).length());
out.close();
tofile.close();
}
catch(IOException e2){}
}
text_event = 0; text.setText(null); setTitle("新建文本文档");
}
else if(ok==JOptionPane.NO_OPTION)
{ text_event = 0; text.setText(null); setTitle("新建文本文档"); }
else if(ok==JOptionPane.CANCEL_OPTION) {text_event = 0; }
}
else { text_event = 0; text.setText(null); setTitle("新建文本文档"); }
}
else if(e.getSource()==itemLoad)
{
if( text_event == 1)
{
String message = "文本内容被修改,是否保存?";
int ok = JOptionPane.showConfirmDialog(null,message,"确认对话框",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
filedialog_Save.setVisible(true);
if(filedialog_Save.getFile()!=null)
{
try{
File file =new File(filedialog_Save.getDirectory(),filedialog_Save.getFile());
tofile=new FileWriter(file);
out =new BufferedWriter(tofile);
out.write(text.getText(),0,(text.getText()).length());
out.close();
tofile.close();
}
catch(IOException e2){}
}
text_event = 0;
}
else if(ok==JOptionPane.NO_OPTION)
{
filedialog_Load.setVisible(true);
text.setText(null);
String s;
if(filedialog_Load.getFile()!=null)
{
try{
File file =new File(filedialog_Load.getDirectory(),filedialog_Load.getFile());
file_reader=new FileReader(file);
setTitle(filedialog_Load.getFile());
in =new BufferedReader(file_reader);
while((s=in.readLine())!=null)
text.append(s+'\n');
in.close();
file_reader.close();
}
catch(IOException e2){}
}
text_event = 0;
}
else if(ok==JOptionPane.CANCEL_OPTION) {}
}
else
{
filedialog_Load.setVisible(true);
text.setText(null);
String s;
if(filedialog_Load.getFile()!=null)
{
try{
File file =new File(filedialog_Load.getDirectory(),filedialog_Load.getFile());
file_reader=new FileReader(file);
setTitle(filedialog_Load.getFile());
in =new BufferedReader(file_reader);
while((s=in.readLine())!=null)
text.append(s+'\n');
in.close();
file_reader.close();
}
catch(IOException e2){}
}
}
}
else if(e.getSource()==itemSave)
{
filedialog_Save.setVisible(true);
if(filedialog_Save.getFile()!=null)
{
try{
File file =new File(filedialog_Save.getDirectory(),filedialog_Save.getFile());
tofile=new FileWriter(file);
out =new BufferedWriter(tofile);
out.write(text.getText(),0,(text.getText()).length());
out.close();
tofile.close();
}
catch(IOException e2){}
}
}
else if(e.getSource()==itemSaveAs)
{
filedialog_Save.setVisible(true);
if(filedialog_Save.getFile()!=null)
{
try{
File file =new File(filedialog_Save.getDirectory(),filedialog_Save.getFile());
setTitle(filedialog_Save.getFile());
tofile=new FileWriter(file);
out =new BufferedWriter(tofile);
out.write(text.getText(),0,(text.getText()).length());
out.close();
tofile.close();
}
catch(IOException e2){}
}
}
else if(e.getSource()==itemExit)
{
System.exit(0);
}
else if(e.getSource()==itemFont)
{
FontDialog dialog =new FontDialog(this,"字体",true);
dialog.setVisible(true);
}
else if(e.getSource()==itemCut)
{
text.cut();
}
else if(e.getSource()==itemCopy)
{
text.copy();
}
else if(e.getSource()==itemPaste)
{
text.paste();
}
else if(e.getSource()==itemRemove)
{
int start = text.getSelectionStart();
int end = text.getSelectionEnd();
text.replaceRange("",start,end);
}
else if(e.getSource()==itemSelectAll)
{
text.selectAll();
}
else if(e.getSource()==itemTime)
{
nowTime = new Date();
time = new SimpleDateFormat("北京时间:yyyy年MM月dd日 E HH时mm分ss秒");
text.insert(time.format(nowTime),text.getCaretPosition());
}
else if(e.getSource()==itemLineWrap)
{
if((clickTimes%2)!=0)
{
text.setLineWrap(true);
text.setWrapStyleWord(true);
clickTimes++;
}
else
{
text.setLineWrap(false);
text.setWrapStyleWord(false);
clickTimes++;
}
}
else if(e.getSource()==itemAbout)
{
AboutDialog about=new AboutDialog(this,"关于",true);
about.setVisible(true);
}
}
public void changedUpdate(DocumentEvent e)
{
text_event = 1;
}
public void insertUpdate(DocumentEvent e){ text_event = 1; }
public void removeUpdate(DocumentEvent e){ text_event = 1; }
public void run(){}
class FontDialog extends Dialog implements ItemListener,ActionListener
{
Choice choiceFont,choiceSize,choiceStyle;
Panel panelButton,panelStyle,panelColor;
JScrollPane scrollPaneModel;
Button yes,no,foreColor,backgroundColor;
String stringName,stringSize,fontstyle;
int size,style;
Font f;
JTextArea model;
CheckboxGroup g;
Checkbox 常规,粗体,斜体,粗斜体;
//风格窗口的定义
FontDialog(Frame f,String s,boolean b)
{
super(f,s,b);
setBounds(60,60,400,300);
setLayout(null);
choiceFont =new Choice();
choiceFont.setBounds(30,40,120,130);
choiceSize =new Choice();
choiceSize.setBounds(180,40,120,130);
choiceStyle =new Choice();
panelButton = new Panel();
panelButton.setBounds(130,240,300,300);
model = new JTextArea("中国China#=+");//示例文本风格
scrollPaneModel = new JScrollPane(model);
scrollPaneModel.setBounds(160,100,160,100);
panelStyle = new Panel();
panelStyle.setBounds(20,80,80,120);
g = new CheckboxGroup();
常规 = new Checkbox("常规",true,g);
粗体 = new Checkbox("粗体",false,g);
斜体 = new Checkbox("斜体",false,g);
粗斜体 = new Checkbox("粗斜体",false,g);
//按钮的定义
yes=new Button("确定");
no=new Button("取消");
panelColor = new Panel();
panelColor.setBounds(30,200,80,60);
foreColor = new Button("设置字体颜色");
foreColor.setBounds(30,200,80,20);
backgroundColor = new Button("设置背景颜色");
backgroundColor.setBounds(30,220,75,20);
//字体的大小
String size[] ={"1","5","9","11","12","13","14","16","18","20","22","24","26","28","36","72"};
for(int i=0;i<size.length;i++)
{
choiceSize.add(size[i]);
}
GraphicsEnvironment ge =GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontName[] = ge.getAvailableFontFamilyNames();
for(int i=0;i<fontName.length;i++)
{
choiceFont.add(fontName[i]);
}
//字体的风格
String fontStyle[] = { "常规","粗体","斜体","粗斜体"};
for(int i=0;i<fontStyle .length;i++)
{
choiceStyle.add(fontStyle [i]);
}
//添加相应的按钮
add(choiceFont);
add(choiceSize);
add(scrollPaneModel);
add(panelButton);
add(panelStyle);
panelStyle.add(常规);
panelStyle.add(粗体);
panelStyle.add(斜体);
panelStyle.add(粗斜体);
panelButton.add(yes);
panelButton.add(no);
add(panelColor);
panelColor.add(foreColor);
panelColor.add(backgroundColor);
choiceFont.addItemListener(this);
choiceSize.addItemListener(this);
常规.addItemListener(this);
粗体.addItemListener(this);
斜体.addItemListener(this);
粗斜体.addItemListener(this);
//按钮事件响应
yes.addActionListener(this);
no.addActionListener(this);
foreColor.addActionListener(this);
backgroundColor.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ setVisible(false); }
});
validate();
}
public void itemStateChanged(ItemEvent e)
{
stringName =choiceFont.getSelectedItem();
stringSize=choiceSize.getSelectedItem();
size=Integer.parseInt(stringSize);
Checkbox box = (Checkbox)e.getSource();
if(box==常规){ g.setSelectedCheckbox(常规); style = Font.TRUETYPE_FONT; }
else if(box==粗体){ g.setSelectedCheckbox(粗体); style = Font.BOLD; }
else if(box==斜体){ g.setSelectedCheckbox(斜体); style = Font.ITALIC; }
else if(box==粗斜体){ g.setSelectedCheckbox(粗斜体); style = Font.BOLD +Font.ITALIC ; }
f =new Font(stringName,style,size);
model.setFont(f);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==yes)
{
text.setFont(f);
setVisible(false);
}
else if(e.getSource()==no)
setVisible(false);
else if(e.getSource()==foreColor)
{
Color foreColor = JColorChooser.showDialog(this,"设置字体颜色",text.getForeground());
if(foreColor!= null)
text.setForeground(foreColor);
}
else if(e.getSource()==backgroundColor)
{
Color backgroundColor = JColorChooser.showDialog(this,"设置背景颜色",text.getBackground());
if(backgroundColor != null)
text.setBackground(backgroundColor);
}
}
}
}
//文件AboutDialog.java
class AboutDialog extends Dialog implements ActionListener
{
AboutDialog(Frame f,String s,boolean b)
{
super(f,s,b);
setBounds(200,200,300,200);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ setVisible(false); }
});
}
public void actionPerformed(ActionEvent e)
{
}
}
//主类NotePad文件NotePad.java
public class NotePad
{
public static void main(String args[])
{
MainWindow win =new MainWindow("新建文本文档");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -