📄 texteditorexample.java
字号:
Point point = text.getSelectionRange();
for (int i = point.x; i < point.x + point.y; i++) {
// 获得选中的字体样式和范围
range = text.getStyleRangeAtOffset(i);
// 如果字体设置了其它样式(如加粗、斜体、加下划线)
if (range != null) {
/**
* 设置一个与原来StyleRange的值相同的StyleRange
*/
style = (StyleRange) range.clone();
style.start = i;
style.length = 1;
// 设置前景颜色
style.foreground = color;
} else {
style = new StyleRange(i, 1, color, null, SWT.NORMAL);
}
text.setStyleRange(style);
}
}
}
}
class SelectAllAction extends Action {
public SelectAllAction() {
super("全选@Alt+A", Action.AS_PUSH_BUTTON);
}
public void run() {
text.selectAll();
}
}
class FormateAction extends Action {
public FormateAction() {
super("格式化@Ctrl+W", Action.AS_CHECK_BOX);
}
public void run() {
text.setWordWrap(isChecked());
}
}
class AboutAction extends Action {
public AboutAction() {
super("关于@Ctrl+H", Action.AS_PUSH_BUTTON);
}
public void run() {
MessageBox messageBox = new MessageBox(getShell(),
SWT.ICON_INFORMATION | SWT.OK);
messageBox.setMessage("文本编辑器2.0版本!");
messageBox.open();
}
}
class BlodAction extends Action {
public BlodAction() {
setText("加粗");
try {
// 载入图像
ImageDescriptor icon = ImageDescriptor.createFromURL(new URL(
"file:icons/blod.bmp"));
setImageDescriptor(icon);
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
}
public void run() {
Point point= text.getSelectionRange();
for (int i = point.x; i < point.x + point.y; i++) {
StyleRange range = text.getStyleRangeAtOffset(i);
if (range != null) {
style = (StyleRange) range.clone();
style.start = i;
style.length = 1;
} else {
style = new StyleRange(i, 1, null, null, SWT.NORMAL);
}
//加粗字体
style.fontStyle ^= SWT.BOLD;
text.setStyleRange(style);
}
}
}
class ItalicAction extends Action {
public ItalicAction() {
setText("斜体");
try {
// 载入图像
ImageDescriptor icon = ImageDescriptor.createFromURL(new URL(
"file:icons/italic.bmp"));
setImageDescriptor(icon);
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
}
public void run() {
Point point = text.getSelectionRange();
for (int i = point.x; i < point.x + point.y; i++) {
range = text.getStyleRangeAtOffset(i);
if (range != null) {
style = (StyleRange) range.clone();
style.start = i;
style.length = 1;
} else {
style = new StyleRange(i, 1, null, null, SWT.NORMAL);
}
//设置为斜体
style.fontStyle ^= SWT.ITALIC;
text.setStyleRange(style);
}
}
}
class UnderlineAction extends Action {
public UnderlineAction() {
setText("下划线");
try {
// 载入图像
ImageDescriptor icon = ImageDescriptor.createFromURL(new URL(
"file:icons/underline.bmp"));
setImageDescriptor(icon);
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
}
public void run() {
Point point = text.getSelectionRange();
for (int i = point.x; i < point.x + point.y; i++) {
range = text.getStyleRangeAtOffset(i);
if (range != null) {
style = (StyleRange) range.clone();
style.start = i;
style.length = 1;
} else {
style = new StyleRange(i, 1, null, null, SWT.NORMAL);
}
//设置下划线
style.underline = !style.underline;
text.setStyleRange(style);
}
}
}
boolean judgeTextSave() {
if (!changes)
return true;
MessageBox messageBox = new MessageBox(getShell(), SWT.ICON_WARNING
| SWT.YES | SWT.NO | SWT.CANCEL);
messageBox.setMessage("是否保存对文件的更改?");
messageBox.setText("文本编辑器V2.0");
int message = messageBox.open();
if (message == SWT.YES) {
return saveTextFile();
} else if (message == SWT.NO) {
return true;
} else {
return false;
}
}
boolean OpenTextFile() {
// 定义对话框,类型为打开型
FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
// 设置对话框打开的限定类型
dialog.setFilterExtensions(new String[] { "*.txt", "*.*" });
// 打开对话框,并返回打开文件的路径
String openFile = dialog.open();
if (openFile == null) {
return false;
}
/**
* java.io包的File类专门处理文件,并获取文件相关信息。
* File类的构造方法: public File(String pathnames)
* 例如 :File file=new(D:\my.java)
* public File(File parent,String child)
* 例如: File file=new(parent,"my.java")
* parent指文件所在目录的文件对象
* public File(String parent,String child)
* 例如:File file=new(dir,"my.java")
* dir指文件所在目录的字符串
*/
file = new File(openFile);
try {
/**
* FileReader文件字符流
* 构造方法: public FileReader(File file)
* 例如:FileReader fileReade=new FileReader(file)
* public FileReader(String filenames)
* filenames为包含文件名的字符串
*/
FileReader fileReader = new FileReader(file);
/**
* BufferedReader类用来把字符流的字符读入缓冲区
*/
BufferedReader reader = new BufferedReader(fileReader);
/**
* 对Stringbuffer字符串缓冲实例化
*/
StringBuffer sb = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
/**
* 通过append()方法实现将字符串添加到字符缓冲区。
* 也可以通过insert()方法将字符串插入缓冲区中
*/
sb.append(line);
sb.append("\r\n");
}
text.setText(sb.toString());
return true;
} catch (IOException e) {
}
return false;
}
boolean saveTextFile() {
if (file == null) {
// 定义文件选择对话框,类型为保存型
FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
dialog.setText("保存");
// 设置对话框保存的限定类型
dialog.setFilterExtensions(new String[] { "*.txt", "*.doc",
"*.xls", "*.*" });
// 打开对话框,并返回保存文件的路径
String saveFile = dialog.open();
if (saveFile == null) {
return false;
}
file = new File(saveFile);
}
try {
FileWriter writer = new FileWriter(file);
writer.write(text.getText());
writer.close();
changes = false;
return true;
} catch (IOException e) {
}
return false;
}
boolean saveFileAs() {
SafeSaveDialog dlg = new SafeSaveDialog(getShell());
String temp = dlg.open();
if (temp == null) {
return false;
}
file = new File(temp);
try {
FileWriter writer = new FileWriter(file);
writer.write(text.getText());
writer.close();
} catch (IOException e) {
}
return false;
}
class SafeSaveDialog {
private FileDialog dlg;
public SafeSaveDialog(Shell shell) {
dlg = new FileDialog(shell, SWT.SAVE);
dlg.setFilterExtensions(new String[] { "*.txt", "*.doc", "*.xls",
"*.*" });
}
public String open() {
String fileName = null;
boolean done = false;
while (!done) {
// 打开另存为对话框,并返回保存路径
fileName = dlg.open();
if (fileName == null) {
done = true;
} else {
// 判断保存的文件是否已经存在
File file = new File(fileName);
if (file.exists()) {
// 若文件存在,则弹出提示性的对话框
MessageBox mb = new MessageBox(dlg.getParent(),
SWT.ICON_WARNING | SWT.YES | SWT.NO);
// 提示性的信息
mb.setMessage(fileName + "已经存在,是否将该文件替换?");
/**
* 单击“yes”按钮这将磁盘上的文件替换
* 否则重新填写文件名
*/
done = mb.open() == SWT.YES;
} else {
done = true;
}
}
}
return fileName;
}
}
public static void main(String[] args) {
new TextEditorExample().run();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -