📄 wordpad.java
字号:
void Save()
{
if(File!=null)
SaveAssist(File);
else if(File==null)
SaveAs();
}
void SaveAs()
{
JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new FileFilterr(".txt","文本文档(*.txt)"));
fc.addChoosableFileFilter(new FileFilterr(".doc","Word 文档(*.doc)"));
if(fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
{
File file = new File(fc.getCurrentDirectory(),fc.getSelectedFile().getName());
if(file.exists())
{
int i = JOptionPane.showConfirmDialog(this,file.getPath() + " 已存在。\n" + "要替换它吗?","保存为",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(i==0)
{
SaveAssist(file);
File = file;
}
else if(i==1)
SaveAs();
}
else
{
SaveAssist(file);
File = file;
}
}
}
void OpenAssist()
{
JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new FileFilterr(".txt","文本文档(*.txt)"));
fc.addChoosableFileFilter(new FileFilterr(".doc","Word 文档(*.doc)"));
fc.addChoosableFileFilter(new FileFilterr("*.*","所有文件(*.*)"));
if(fc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
File file = new File(fc.getCurrentDirectory(),fc.getSelectedFile().getName());
if(file.exists())
{
try
{
// FileInputStream fis = new FileInputStream(file);
// ObjectInputStream ois = new ObjectInputStream(fis);
// sd = (StyledDocument) ois.readObject();
// saved = true;
// validate();
// }
BufferedReader br = new BufferedReader(new FileReader(file));
String string;
while ((string = br.readLine())!=null)
TextArea.setText(TextArea.getText() + string);
br.close();
TextArea.setCaretPosition(0);
}
catch(IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}
// catch (ClassNotFoundException cnfe)
// {
// System.err.println("Class not found: " + cnfe.getMessage());
// }
}
else
{
JOptionPane.showMessageDialog(null,"fc.getSelectedFile().getName()\n文件不存在\n请检查所给的文件名是否正确","错误",0);
}
}
}
void Open()
{
if(saved==true)
{
OpenAssist();
}
else if(File==null && TextArea.getText().equals(""))
{
OpenAssist();
}
else if(File==null)
{
int i = JOptionPane.showConfirmDialog(this,"将改动保存到 文档?","计事本",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(i==0)
{
SaveAs();
OpenAssist();
}
else if(i==1)
{
OpenAssist();
}
}
else if(File!=null)
{
int i = JOptionPane.showConfirmDialog(this,"将改动保存到" + File.getPath() + "文档?","WordPad",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(i==0)
{
Save();
OpenAssist();
}
else if(i==1)
OpenAssist();
}
}
void Exit()
{
if(saved)
System.exit(0);
else if(File==null && TextArea.getText().equals(""))
System.exit(0);
else if(File==null)
{
int i = JOptionPane.showConfirmDialog(this,"将改动保存到 文档?","WordPad",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(i==0)
{
Save();
System.exit(0);
}
else if(i==1)
System.exit(0);
}
else if(File!=null)
{
int i = JOptionPane.showConfirmDialog(this,"将改动保存到" + File.getName() + "文档?","WordPad",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(i==0)
{
Save();
System.exit(0);
}
else if(i==1)
System.exit(0);
}
}
void setCharacterAttributes(AttributeSet s)
{
int start = TextArea.getSelectionStart();
int end = TextArea.getSelectionEnd();
if(start!=end)
{
sd.setCharacterAttributes(start, end-start, s, false);
// TextArea.setCaretPosition(start);
}
// else if(start==end)
// {
// TextArea.setCharacterAttributes(s, true);
// TextArea.setCaretPosition(start);
// }
getStyledEditorKit().getInputAttributes().addAttributes(s);
}
StyledEditorKit getStyledEditorKit()
{
EditorKit ek = TextArea.getEditorKit();
if (ek instanceof StyledEditorKit)
return (StyledEditorKit) ek;
throw new IllegalArgumentException("Only StyledEditorKit!");
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==File_New || e.getSource()==Tool_New)
New();
else if(e.getSource()==File_Open || e.getSource()==Tool_Open)
Open();
else if(e.getSource()==File_Save || e.getSource()==Tool_Save)
Save();
else if(e.getSource()==File_SaveAs)
SaveAs();
else if(e.getSource()==File_Exit)
Exit();
if(e.getSource()==Help_About)
JOptionPane.showMessageDialog(null,"张惟伊倾情钜献\nProduced by ZV!","关于写字板",3,new ImageIcon("pic/mili.jpg"));
else if(e.getSource()==Format_Auto)
{
// if(Format_Auto.isSelected())
// TextArea.setLineWrap(true);
// else
// TextArea.setLineWrap(false);
}
else if(e.getSource()==Edit_Undo || e.getSource()==Tool_Undo)
{
if(um.canUndo())
um.undo();
}
else if(e.getSource()==Edit_Redo || e.getSource()==Tool_Redo)
{
if(um.canRedo())
um.redo();
}
else if(e.getSource() == Edit_Cut || e.getSource() == Tool_Cut)
TextArea.cut();
else if(e.getSource() == Edit_Copy || e.getSource() == Tool_Copy)
TextArea.copy();
else if(e.getSource() == Edit_Paste || e.getSource() == Tool_Paste)
TextArea.paste();
else if(e.getSource() == Edit_Del || e.getSource() == Tool_Delete)
TextArea.replaceSelection("");
else if(e.getSource() == Edit_SelectAll)
TextArea.selectAll();
else if(e.getSource() == Insert_DateTime ||e.getSource()==Tool_DateTime)
{
Date d = new Date();
TextArea.setText(TextArea.getText() + d.toString());
}
else if(e.getSource()==Insert_Picture)
{
JFileChooser jfc = new JFileChooser();
jfc.addChoosableFileFilter(new FileFilterr(".bmp","位图(*.bmp)"));
jfc.addChoosableFileFilter(new FileFilterr(".jpg","JPEG(*.jpg)"));
jfc.addChoosableFileFilter(new FileFilterr(".gif","GIF(*.gif)"));
if(jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
TextArea.insertIcon(new ImageIcon(jfc.getSelectedFile().getPath()));
}
else if(e.getSource()==View_Tool)
{
if(View_Tool.isSelected())
ToolBar.setVisible(true);
else
ToolBar.setVisible(false);
}
else if(e.getSource()==View_Format)
{
if(View_Format.isSelected())
FormatBar.setVisible(true);
else
FormatBar.setVisible(false);
}
else if(e.getSource()==Format_Font)
{
Fontt = new Fontt(this,Font);
TextArea.setFont(Fontt.Font);
}
else if(e.getSource()==FormatBar_Font)
{
MutableAttributeSet sas=new SimpleAttributeSet();
StyleConstants.setFontFamily(sas, (String)FormatBar_Font.getSelectedItem());
setCharacterAttributes(sas);
}
else if(e.getSource()==FormatBar_Color || e.getSource()==Format_FontColor)
{
MutableAttributeSet sas = getStyledEditorKit().getInputAttributes();
JColorChooser jcc = new JColorChooser();
StyleConstants.setForeground(sas, Color = jcc.showDialog(this, "颜色", Color));
setCharacterAttributes(sas);
}
else if(e.getSource()==FormatBar_Size)
{
MutableAttributeSet sas = getStyledEditorKit().getInputAttributes();
StyleConstants.setFontSize(sas,new Integer((String)FormatBar_Size.getSelectedItem()));
setCharacterAttributes(sas);
}
else if(e.getSource()==FormatBar_Bold)
{
MutableAttributeSet sas = getStyledEditorKit().getInputAttributes();
boolean bold = (StyleConstants.isBold(sas)) ? false : true;
StyleConstants.setBold(sas, bold);
setCharacterAttributes(sas);
}
else if(e.getSource()==FormatBar_Itical)
{
MutableAttributeSet sas = getStyledEditorKit().getInputAttributes();
boolean itical = (StyleConstants.isItalic(sas)) ? false : true;
StyleConstants.setItalic(sas, itical);
setCharacterAttributes(sas);
}
else if(e.getSource()==FormatBar_U)
{
MutableAttributeSet sas = getStyledEditorKit().getInputAttributes();
boolean underline = (StyleConstants.isUnderline(sas)) ? false : true;
StyleConstants.setUnderline(sas, underline);
setCharacterAttributes(sas);
}
else if(e.getSource()==FormatBar_Left)
{
MutableAttributeSet sas = getStyledEditorKit().getInputAttributes();
StyleConstants.setAlignment(sas, 0);
setCharacterAttributes(sas);
}
else if(e.getSource()==FormatBar_Center)
{
MutableAttributeSet sas = getStyledEditorKit().getInputAttributes();
StyleConstants.setAlignment(sas, 1);
setCharacterAttributes(sas);
}
else if(e.getSource()==FormatBar_Right)
{
MutableAttributeSet sas = getStyledEditorKit().getInputAttributes();
StyleConstants.setAlignment(sas, 2);
setCharacterAttributes(sas);
}
else if(e.getSource()==Edit_Find || e.getSource()==Tool_Find)
Find = new Find(this);
else if(e.getSource()==Edit_Replace)
Replace = new Replace(this);
}
public void insertUpdate(DocumentEvent de)
{
saved = false;
File_Save.setEnabled(true);
Tool_Save.setEnabled(true);
}
public void removeUpdate(DocumentEvent de)
{
saved=false;
File_Save.setEnabled(true);
Tool_Save.setEnabled(true);
}
public void changedUpdate(DocumentEvent de)
{
saved=false;
File_Save.setEnabled(true);
Tool_Save.setEnabled(true);
}
public void undoableEditHappened(UndoableEditEvent uee)
{
um.addEdit(uee.getEdit());
}
public static void main(String[] args)
{
WordPad z=new WordPad();
z.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -