📄 texteditor.java
字号:
insertDoc(styledDoc,display.getSelectedText(),"style");
}
} else if(command=="MOTIF风格"){
try {
UIManager.setLookAndFeel(looks[1].getClassName());
SwingUtilities.updateComponentTreeUI( frame );
}catch ( Exception ea ) {}
}else if(command=="默认风格"){
try {
UIManager.setLookAndFeel(looks[0].getClassName());
SwingUtilities.updateComponentTreeUI(frame);
}catch ( Exception eb ) {}
}else if(command=="系统风格"){
try {
UIManager.setLookAndFeel(looks[3].getClassName());
SwingUtilities.updateComponentTreeUI( frame );
}catch ( Exception ea ) {}
}
}
}
/** 内部类viewAction,用来处理查看菜单事件 **/
public class viewAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
int length=0,blank=0,row=0,columns=1;
String space="",line="",text="",other=" ";
if(command=="字数统计(W)"){
if (display.getSelectedText() == null)
text=display.getText();
else text=display.getSelectedText();
length=text.length();
for(int i=0;i<length;i++)
if(text.charAt(i)==' ') blank++; //比较字符是不是空格
else if(String.valueOf(text.charAt(i)).equals("\n"))
row++;
for(int i=0;i<70;i++){
space+=" ";
if(i<50)
line+="-";
}
String message="\n字数"+other+space+(length-2*row-blank)+
"\n字符数"+space+(length-2*row)+
"\n空格数"+space+blank+
"\n行数"+other+space+(row+1);
Object[] options = {"关闭"};
JOptionPane.showOptionDialog(frame,"统计信息:"+line+message, "字数统计",
JOptionPane.DEFAULT_OPTION,JOptionPane.CLOSED_OPTION,null, options,options[0]);
}
}
}
/** 内部类helpAction,用来处理帮助菜单事件 **/
public class helpAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command=e.getActionCommand();
if(command=="关于(A)")
new AboutDialog(frame,"关干 文本编辑器",true);
else if(command=="帮助主题(H)")
new HelpTheme();
}
}
/** 内部类mouseAction,用来处理鼠标事件 **/
public class mouseAction extends MouseAdapter {
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger())
popupmenu.show(display, e.getX(), e.getY());//显示右键菜单
}
}
/**内部类documentlis用来处理文档内容变化事件***/
public class documentLis implements DocumentListener{
public void changedUpdate(DocumentEvent arg0) {
change=true;
}
public void insertUpdate(DocumentEvent arg0) {
change=true;
undoItem.setEnabled(true);
findItem.setEnabled(true);
findnextItem.setEnabled(true);
}
public void removeUpdate(DocumentEvent arg0) {
change=true;
}
}
/***********************************************
* Note类里面的方法操作
* ****************************************************/
public void insertIcon(File file) { //插入图片的方法
display.setCaretPosition(styledDoc.getLength()); // 设置插入位置
display.insertIcon(new ImageIcon(file.getPath())); // 插入图片
}
public void insertDoc(StyledDocument styledDoc, String content,String currentStyle) {
try {
int len=display.getSelectedText().length();
styledDoc.insertString(display.getCaretPosition(),content,styledDoc.getStyle(currentStyle));
display.replaceSelection("");
display.select(display.getCaretPosition()-len,display.getCaretPosition());
} catch (Exception e) {}
}
public void createStyle(String style,StyledDocument doc,int size,boolean bold,boolean italic,Color color,String fontName) {
Style sys = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
try {
doc.removeStyle(style);
} catch(Exception e) {} //先删除这种Style,假使他存在
Style s = doc.addStyle(style,sys); // 加入
StyleConstants.setFontSize(s,size); // 大小
StyleConstants.setBold(s,bold); // 粗体
StyleConstants.setItalic(s,italic); // 斜体
StyleConstants.setForeground(s,color); // 颜色
StyleConstants.setFontFamily(s,fontName); // 字体
}
public void openFile(){ //打开文件
JFileChooser open= new JFileChooser();
open.addChoosableFileFilter(new JavaFileFilter());
open.addChoosableFileFilter(new TxtFileFilter());
// 设置默认的文件管理器。如果不设置,则最后添加的文件过滤器为默认过滤器.这里设置为txtFilter
int state = open.showOpenDialog(frame);// 弹出文件选择窗口,点击确定返回一个整数
File file = open.getSelectedFile();
/*JFileChooser.APPROVE_OPTION常量存储着确定按钮对应的整数*/
if (file != null && state == JFileChooser.APPROVE_OPTION){
currentFile=file;
display.setText("");
frame.setTitle(file.getName()+" - 文字编辑");
try {
FileReader readFile=new FileReader(file.getAbsolutePath());//文件的读取
int len=(int)file.length();
char []buffer=new char[len];
readFile.read(buffer,0,len);
display.setText(new String(buffer));
readFile.close();
} catch (Exception e) { }
change=false; //打开一个文件之后把文档内容变化标记变量设为false
}
}
public void saveAsFile() { //另存为
JFileChooser save=new JFileChooser();
save.addChoosableFileFilter(new JavaFileFilter());
save.addChoosableFileFilter(new TxtFileFilter());
save.setDialogTitle("另存为");
int state = save.showSaveDialog(frame);
File file=save.getSelectedFile();
if (file != null && state == JFileChooser.APPROVE_OPTION){
/**正则表达式用来查看输入的文件名是否有后缀名 */
Pattern pattern=Pattern.compile(".*\\.[a-z]+",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(file.getAbsolutePath());
if(!matcher.find()){
file=new File(file.getAbsolutePath().concat(".txt"));
}
currentFile=file;
try {
FileWriter target = new FileWriter(currentFile.getAbsolutePath());
target.write(display.getText());
frame.setTitle(currentFile.getName()+ " - 文字编辑");
target.close();
} catch (Exception e) { }
change=false;
}
}
public void saveFile(){ //保存
if (currentFile != null){
try{
FileWriter file = new FileWriter(currentFile);
file.write(display.getText());
file.close();
}catch(Exception e){ }
change=false;
}else saveAsFile();
}
public void exitText(){ //退出文本的相关操作
String path="无标题";
if(currentFile!=null)
path=currentFile.getAbsolutePath();
int num =JOptionPane.showConfirmDialog(container,"文件 "+ path +" 的文字已经改变。想要保存文件吗?", "文字编辑",
JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(num==0) saveFile();
else if(num==1&&newflag){frame.dispose();new Note();}
else if(num==1&&exitflag){frame.dispose();}
else if(num==1&&windowflag){System.exit(0);}
else if(num==1&&openflag) openFile();
}
}
/*********用干文件过滤的外部类***********/
class GifFileFilter extends FileFilter{
public String getDescription(){
return "gif源文件(*.gif)";
}
public boolean accept(File file) {
return file.getName().toLowerCase().endsWith(".gif");
}
}
class JpgFileFilter extends FileFilter{
public String getDescription(){
return "jpg源文件(*.jpg)";
}
public boolean accept(File file) {
return file.getName().toLowerCase().endsWith(".jpg");
}
}
class JavaFileFilter extends FileFilter {
public String getDescription() {
return "java源文件(*.java)";
}
public boolean accept(File file) {
return file.getName().toLowerCase().endsWith(".java");
}
}
class TxtFileFilter extends FileFilter {
public String getDescription() {
return "文本文档(*.txt)";
}
public boolean accept(File file) {
return file.getName().toLowerCase().endsWith(".txt");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -