本例使用Java Swing技术实现了一个文本编辑器,具有查找、替换、删除、新建、保存、打开等功能,由于本人知识浅陋,字体功能暂未实现,目前程序还有很多待完善以及写得不好的地方,望大家多多指教。
一、问题描述
开发环境:Eclipse2020.12 + jdk1.8.0
二、解题思路
设计两个类:一个是主类(Notepad),一个功能类(Function)
Notepad:主类则为运行程序的入口,调用功能类,完成程序功能main方法,新建Function对象,调用Function类中的方法完成功能。
Function:功能类主要用来实现图形化界面设计及其相关方法。
- private void init(); //初始化面板
顶层容器:JFrame,JDialog
中间容器:JMenuBar,JScrollpane,JPanel
基本组件:JMenu,JMenuItem,JTexArea,JLabel,JButton,JTextField
布局管理器:FlowLayout,BoxLayout
其他:Java事件监听,Java文件读取
- private void event(); //实现面板功能
对文件:新建、保存、打开、关闭
对文本内容:修改、删除、查找、替换
对文本内容的字体、颜色
三、主要源码
1、主程序
public class Notepad {
public static void main(String[] args) {
new Function();
}
}
2、图形化界面设计
public class Function {
private JFrame app;// 窗体
private JMenuBar a;// 菜单栏
private JMenu b, c, d;// 菜单
private JMenuItem b1, b2, b3, b4, c1, c2, d1, d2;// 菜单项
private JTextArea f;// 多行文本
private JScrollPane g;// 滚动条
private FileDialog i;// 对话框
private File m;
int start = 0;// 查找开始位置
int end = 0;// 查找结束位置
private static int w = Toolkit.getDefaultToolkit().getScreenSize().width;// 获取显示器屏幕宽度
private static int h = Toolkit.getDefaultToolkit().getScreenSize().height;// 获取显示器屏幕高度
public Function() {
init();// 初始化面板
}
private void init() {
// 窗体
app = new JFrame("新建文本文档.txt-记事本");// 窗体名字
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 退出窗体
app.setSize(1000, 800);// 窗体大小
app.setLocation((w - 1000) / 2, (h - 800) / 2);// 窗体居中
app.setVisible(true);// 显示窗体
// 菜单栏
a = new JMenuBar();
app.setJMenuBar(a);
// 文件菜单
b = new JMenu("文件(F)");
a.add(b);
b1 = new JMenuItem("新建(N)");
b.add(b1);
b1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));// 设置快捷键
b2 = new JMenuItem("保存(S)");
b.add(b2);
b2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
b3 = new JMenuItem("打开(O)");
b.add(b3);
b3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
b4 = new JMenuItem("关闭(X)");
b.add(b4);
b4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
// 编辑菜单
c = new JMenu("编辑(E)");
a.add(c);
c1 = new JMenuItem("删除(L)");
c.add(c1);
c1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,InputEvent.CTRL_MASK));
c2 = new JMenuItem("查找替换(F)");
c.add(c2);
c2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));
// 格式菜单
d = new JMenu("格式(O)");
a.add(d);
d1 = new JMenuItem("自动换行(W)");
d.add(d1);
d1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,InputEvent.CTRL_MASK));
d2 = new JMenuItem("字体(F)");
d.add(d2);
d2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));
// 多行文本
f = new JTextArea();
g = new JScrollPane(f, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,// 垂直滚动条,总是出现
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);// 水平滚动条,总是出现
f.setFont(new Font("宋体", Font.PLAIN, 20));// 默认字体,宋体20号
app.add(g);
event();
}
运行结果 :
3、界面响应
private void event() {
// 新建
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.setText("");
}
});
// 保存
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 判断是否需要对话框
if (m == null) {
// 创建文件对话框对象
i = new FileDialog(app, "保存文件", FileDialog.SAVE);
i.setVisible(true);
}
// 获取用户要保存的文件
String j = i.getDirectory();// 获取文件路径
String fileName = i.getFile() + ".txt";// 获取文件名
if (j == null || fileName == null)// 用户点击取消
return;
// 用户要保存的文件
m = new File(j, fileName);
// IO流写文件
PrintWriter l = null;
try {
l = new PrintWriter(new FileWriter(m), true);
l.println(f.getText());
} catch (IOException e1) {
throw new RuntimeException();
} finally {
if (l != null)
l.close();
}
}
});
// 打开
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 创建文件对话框对象
i = new FileDialog(app, "打开文件");
i.setVisible(true);
// 获取到用户选择的文件
String j = i.getDirectory();// 获取文件路径
String fileName = i.getFile();// 获取文件名
if (j == null || fileName == null)// 用户点击取消
return;
// 用户点击了确切文件并打开
m = new File(j, fileName);
// IO流读取文件
BufferedReader l = null;
try {
l = new BufferedReader(new FileReader(m));
String line = null;
// 清空多行文本中的数据
f.setText("");
while ((line = l.readLine()) != null) {
f.append(line + "\r\n");// 在文本最后增加换行符
}
} catch (IOException e1) {
throw new RuntimeException();
} finally {
try {
if (l != null)
l.close();
} catch (IOException e1) {
throw new RuntimeException();
}
}
}
});
// 关闭
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// 删除
c1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.requestFocus();
f.replaceRange("", f.getSelectionStart(), f.getSelectionEnd());
}
});
// 查找替换
c2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 对话框
JDialog search=new JDialog( app, "查找和替换");// 标题
search.setSize(320, 100);// 大小
search.setLocation((w - 320) / 2, (h - 100) / 2);// 位置
search.setVisible(true);// 显示
// 布局
JPanel panel = new JPanel(new GridLayout(2,3));
search.add(panel);
// 第一行
JLabel label_1 = new JLabel("查找内容:");
panel.add(label_1);
final JTextField textField_1 = new JTextField(5);
panel.add(textField_1);
JButton buttonFind = new JButton("查找下一个");
panel.add(buttonFind);
// 第二行
JLabel label_2 = new JLabel("替换内容:");
panel.add(label_2);
final JTextField textField_2=new JTextField(5);
panel.add(textField_2);
JButton buttonChange = new JButton("替换");
panel.add(buttonChange);
// 为查找下一个按钮绑定监听事件
buttonFind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String findText = textField_1.getText(); // 查找的字符串
String textArea = f.getText(); // 当前文本框的内容
start = textArea.indexOf(findText,end); // 查找开始位置
end = start+findText.length(); // 查找结束位置
if(start == -1){ // 没有找到
JOptionPane.showMessageDialog(null,"没找到"+findText,"记事本",JOptionPane.WARNING_MESSAGE);
}else{
f.select(start,end);
}
}
});
// 为替换按钮绑定监听事件
buttonChange.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String changeText=textField_2.getText(); // 获取替换的内容
f.replaceSelection(changeText); // 替换
f.select(start,end);
}
});
}
});
// 自动换行
d1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.setLineWrap(true);
}
});
}
}
四、结果展示
文件的打开与保存
删除
查找
替换
自动换行