java文本编辑器
源码里有注释:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
//简单的文本编辑器
public class Editor extends JFrame {
public JTextPane textPane = new JTextPane(); //文本窗格d,编辑窗口
public JFileChooser filechooser = new JFileChooser(); //文件选择器
public Editor()
{
super("简记");
Action[] actions= //菜单项的各种功能
{
new NewAction(),
new OpenAction(),
new SaveAction(),
new CutAction(),
new CopyAction(),
new PasteAction(),
new AboutAction(),
new ExitAction(),
new HelpAction()
};
setJMenuBar(createJMenuBar(actions)); //根据actions创建菜单栏
Container container=getcontentPane();
container.add(textPane, Borderlayout.CENTER);
setSize(1300,1500);
setVisible(true);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JMenuBar createJMenuBar(Action[] actions) //创建菜单栏的函数
{
JMenuBar menubar=new JMenuBar();
JMenu menuFile=new JMenu("文件(F)");
JMenu menuEdit=new JMenu("编辑(E)");
JMenu menuAbout=new JMenu("帮助(H)");
menuFile.add(new JMenuItem(actions[0]));
menuFile.add(new JMenuItem(actions[1]));
menuFile.add(new JMenuItem(actions[2]));
menuFile.add(new JMenuItem(actions[7]));
menuEdit.add(new JMenuItem(actions[3]));
menuEdit.add(new JMenuItem(actions[4]));
menuEdit.add(new JMenuItem(actions[5]));
menuAbout.add(new JMenuItem(actions[6]));
menuAbout.add(new JMenuItem(actions[8]));
menubar.add(menuFile);
menubar.add(menuEdit);
menubar.add(menuAbout);
return menubar;
}
class NewAction extends AbstractAction //新建
{
public NewAction()
{
super("新建(N) Ctrl+N");
}
public void actionPerformed(ActionEvent e)
{
textPane.setDocument(new DefaultStyledDocument());
}
}
class OpenAction extends AbstractAction //打开
{
public OpenAction()
{
super("打开(O) Ctrl+O");
}
public void actionPerformed(ActionEvent e)
{
int i=filechooser.showOpenDialog(Editor.this); //显示打开文件对话框
if(i==JFileChooser.APPROVE_OPTION) //点击对话框打开选项
{
File f=filechooser.getSelectedFile(); //得到选择的文件
try
{
InputStream is=new fileinputstream(f);
textPane.read(is, "d");
}
catch(Exception ex)
{
ex.printstacktrace();
}
}
}
}
class SaveAction extends AbstractAction //保存
{
public SaveAction()
{
super("保存(S) Ctrl+S");
}
public void actionPerformed(ActionEvent e)
{
int i=filechooser.showSaveDialog(Editor.this);
if(i==JFileChooser.APPROVE_OPTION)
{
File f=filechooser.getSelectedFile();
try
{
Fileoutputstream out=new FileOutputStream(f);
out.write(textPane.getText().getBytes());
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}
class ExitAction extends AbstractAction //退出
{
public ExitAction()
{
super("退出(X)");
}
public void actionPerformed(ActionEvent e)
{
dispose();
}
}
class CutAction extends AbstractAction //剪切
{
public CutAction()
{
super("剪切(T) Ctrl+X");
}
public void actionPerformed(ActionEvent e)
{
textPane.cut();
}
}
class CopyAction extends AbstractAction //复制
{
public CopyAction()
{
super("复制(C) Ctrl+C");
}
public void actionPerformed(ActionEvent e)
{
textPane.copy();
}
}
class PasteAction extends AbstractAction //粘贴
{
public PasteAction()
{
super("粘贴(P) Ctrl+V");
}
public void actionPerformed(ActionEvent e)
{
textPane.paste();
}
}
class AboutAction extends AbstractAction
{
public AboutAction()
{
super("关于简记(A)");
}
public void actionPerformed(ActionEvent e)
{
JOptionPane.showmessageDialog(Editor.this,"实现了记事本的一些基本功能","关于",JOptionPane.PLAIN_MESSAGE);
}
}
class HelpAction extends AbstractAction
{
public HelpAction()
{
super("联系开发者");
}
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(Editor.this,"[email protected]","开发者邮箱",JOptionPane.PLAIN_MESSAGE);
}
}
public static void main(String[] args)
{
new Editor();
}
}
相关阅读
工程师为消费类设备开发了这种语言,并使其与当时适度的CPU兼容时保持了简单性。从那时起,这种面向对象的语言已用于创建简单到
JavaScript主要作用是什么呢?学习编程的同学对JavaScript并不陌生,JavaScript是前端技术中非常重要的内容,是网站搭建必不可少的
java.lang.UnsupportedClassVersionError
截图: 错误日志: Exception in thr
学了一段时间Java了,但是还是很菜,看到反射这一节的时候,就有点厌烦,看不下去了,过了一段时间后我又翻了回来,因为要学习后面的,所以反射
Java 混淆器就是给.class加密以防止反编译的工具 开源的 RetroGuard http://www.retrologic.com/ IBM的 JAX