📄 texteditorexample.java
字号:
/**
*@author: WangJinTao,MengQingChang2006
*/
package swtjfaceExample;
import java.io.*;
import java.net.*;
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.jface.action.*;
import org.eclipse.jface.resource.*;
import org.eclipse.jface.window.*;
public class TextEditorExample extends ApplicationWindow {
private StyledText text;
private Action newCreate;
private Action openFile;
private Action saveFile;
private Action saveAsFile;
private Action exit;
private Action copyFile;
private Action pasteFile;
private Action cutFile;
private Action setFont;
private Action setColor;
private Action selectAll;
private Action formate;
private Action about;
private Font font;
private File file;
private Color color;
private StyleRange style, range;
boolean changes;;
TextEditorExample() {
// 部署窗口
super(null);
newCreate = new NewCreateAction();
openFile = new OpenFileAction();
saveFile = new SaveFileAction();
saveAsFile = new SaveAsFileAction();
exit = new ExitAction();
copyFile = new CopyFileAction();
pasteFile = new PasteFileAction();
cutFile = new CutFileAction();
setFont = new SetFontAction();
setColor = new SetColorAction();
selectAll = new SelectAllAction();
formate = new FormateAction();
about = new AboutAction();
addMenuBar();
addToolBar(SWT.FLAT);
}
public void run() {
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
public Control createContents(Composite parent) {
// 设置窗体大小
parent.getShell().setSize(520, 370);
// 设置窗体标题
parent.getShell().setText("TextEditor实例");
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, true));
text = new StyledText(composite, SWT.BORDER | SWT.V_SCROLL
| SWT.H_SCROLL);
text.setLayoutData(new GridData(GridData.FILL_BOTH));
text.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
changes = true;
}
});
return composite;
}
protected MenuManager createMenuManager() {
MenuManager menuBar = new MenuManager();
MenuManager fileMenu = new MenuManager("文件(&F)");
MenuManager editorMenu = new MenuManager("编辑(&E)");
MenuManager helpMenu = new MenuManager("帮助(&H)");
// 在文件菜单项添加下拉菜单
fileMenu.add(newCreate);
fileMenu.add(openFile);
fileMenu.add(new Separator());
fileMenu.add(saveFile);
fileMenu.add(saveAsFile);
fileMenu.add(new Separator());
fileMenu.add(exit);
// 在编辑菜单下添加下拉菜单
editorMenu.add(copyFile);
editorMenu.add(pasteFile);
editorMenu.add(cutFile);
editorMenu.add(new Separator());
editorMenu.add(setFont);
editorMenu.add(setColor);
editorMenu.add(new Separator());
editorMenu.add(selectAll);
editorMenu.add(formate);
helpMenu.add(about);
// 在menuBar上添加文件菜单、编辑菜单和帮助菜单
menuBar.add(fileMenu);
menuBar.add(editorMenu);
menuBar.add(helpMenu);
return menuBar;
}
// 在工具栏上添加工具栏按钮
protected ToolBarManager createToolBarManager(int style) {
ToolBarManager toolBarManager = new ToolBarManager(style);
toolBarManager.add(new NewCreateAction());
toolBarManager.add(new OpenFileAction());
toolBarManager.add(new SaveFileAction());
toolBarManager.add(new Separator());
toolBarManager.add(new CopyFileAction());
toolBarManager.add(new PasteFileAction());
toolBarManager.add(new CutFileAction());
toolBarManager.add(new Separator());
toolBarManager.add(new BlodAction());
toolBarManager.add(new ItalicAction());
toolBarManager.add(new UnderlineAction());
return toolBarManager;
}
class NewCreateAction extends Action {
public NewCreateAction() {
super("NewCreateAction@Ctrl+N", Action.AS_PUSH_BUTTON);
setText("新建");
try {
// 载入图像
ImageDescriptor icon = ImageDescriptor.createFromURL(new URL(
"file:icons/new.gif"));
setImageDescriptor(icon);
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
}
public void run() {
// 在新建文本之前,判断当前文本是否需要保存
if (judgeTextSave()) {
text.setText("");
}
}
}
class OpenFileAction extends Action {
public OpenFileAction() {
super("OpenFileAction@Ctrl+O", Action.AS_PUSH_BUTTON);
setText("打开");
try {
// 载入图像
ImageDescriptor icon = ImageDescriptor.createFromURL(new URL(
"file:icons/open.gif"));
setImageDescriptor(icon);
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
}
public void run() {
// 在打开新的文件之前,判断是否保存当前文件
if (judgeTextSave())
OpenTextFile();
}
}
class SaveFileAction extends Action {
public SaveFileAction() {
super("SaveFileAction@Ctrl+S", Action.AS_PUSH_BUTTON);
setText("保存");
try {
// 载入图像
ImageDescriptor icon = ImageDescriptor.createFromURL(new URL(
"file:icons/save.gif"));
setImageDescriptor(icon);
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
}
public void run() {
saveTextFile();
}
}
class SaveAsFileAction extends Action {
public SaveAsFileAction() {
super("另存为@Ctrl+A", Action.AS_PUSH_BUTTON);
}
public void run() {
saveFileAs();
}
}
class ExitAction extends Action {
public ExitAction() {
super("退出@Ctrl+E", Action.AS_PUSH_BUTTON);
}
public void run() {
getShell().dispose();
}
}
class CopyFileAction extends Action {
public CopyFileAction() {
super("CopyFileAction@Ctrl+C", Action.AS_PUSH_BUTTON);
setText("复制");
try {
// 载入图像
ImageDescriptor icon = ImageDescriptor.createFromURL(new URL(
"file:icons/copy.gif"));
setImageDescriptor(icon);
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
}
public void run() {
text.copy();
}
}
class PasteFileAction extends Action {
public PasteFileAction() {
super("PasteFileAction@Ctrl+V", Action.AS_PUSH_BUTTON);
setText("粘贴");
try {
// 载入图像
ImageDescriptor icon = ImageDescriptor.createFromURL(new URL(
"file:icons/paste.gif"));
setImageDescriptor(icon);
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
}
public void run() {
text.paste();
}
}
class CutFileAction extends Action {
public CutFileAction() {
super("CutFileAction @Ctrl+X", Action.AS_PUSH_BUTTON);
setText("剪切");
try {
// 载入图像
ImageDescriptor icon = ImageDescriptor.createFromURL(new URL(
"file:icons/cut.gif"));
setImageDescriptor(icon);
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
}
public void run() {
text.cut();
}
}
class SetFontAction extends Action {
public SetFontAction() {
super("设置字体@Alt+F", Action.AS_PUSH_BUTTON);
}
public void run() {
FontDialog fontDialog = new FontDialog(getShell());
fontDialog.setFontList((text.getFont()).getFontData());
FontData fontData = fontDialog.open();
if (fontData != null) {
if (font != null) {
font.dispose();
}
font = new Font(getShell().getDisplay(), fontData);
text.setFont(font);
}
}
}
class SetColorAction extends Action {
public SetColorAction() {
super("设置颜色@Alt+C", Action.AS_PUSH_BUTTON);
}
public void run() {
// 定义颜色选择对话框
ColorDialog dlg = new ColorDialog(getShell());
// 打开对话框
RGB rgb = dlg.open();
if (rgb != null) {
// 定义color对象
color = new Color(getShell().getDisplay(), rgb);
// 定义point对象,获取选择范围。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -