JAVA实现简易文本编辑器设计

废话不多说直接上代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.FontUIResource;
import javax.swing.text.Document;
import java.io.*;
import java.util.Enumeration;

public class JTextAreaTester extends JFrame implements ActionListener {
	private JButton btnOpen, btnSave, btnCopy, btnPaste, btnCut, btnSelectAll, btnClearAll;
	private JTextArea ta;
	private boolean isTextModified; // 文本被编辑的标记
	private String file = ""; // 文件名
	private final static String APP_NAME = "简易文本编辑器";

	public JTextAreaTester() {
		super(APP_NAME);
		this.setSize(500, 300);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		ta = new JTextArea();
		ta.setLineWrap(true); // 自动换行
		ta.setFont(new Font("宋体", Font.PLAIN, 16));
		JScrollPane sp = new JScrollPane(ta); // 根据需要自动显示滚动条
		JButton btnOpen = new JButton("打开文件");
		btnOpen.setActionCommand("打开文件");
		JButton btnSave = new JButton("保存文件");
		btnSave.setActionCommand("保存文件");
		JButton btnCopy = new JButton("复制");
		btnCopy.setActionCommand("复制");
		JButton btnPaste = new JButton("粘贴");
		btnPaste.setActionCommand("粘贴");
		JButton btnCut = new JButton("剪切");
		btnCut.setActionCommand("剪切");
		JButton btnSelectAll = new JButton("全选");
		btnSelectAll.setActionCommand("全选");
		JButton btnClearAll = new JButton("清空");
		btnClearAll.setActionCommand("清空");
		// 为组件注册动作事件监听器
		btnOpen.addActionListener(this);
		btnSave.addActionListener(this);
		btnPaste.addActionListener(this);
		btnSelectAll.addActionListener(this);

		JPanel panel = new JPanel(); // 按钮放置在一个面板内
		FlowLayout layout = (FlowLayout) panel.getLayout();
		layout.setHgap(2);
		panel.add(btnOpen);
		panel.add(btnSave);
		panel.add(btnCopy);
		panel.add(btnPaste);
		panel.add(btnCut);
		panel.add(btnSelectAll);
		panel.add(btnClearAll);
		this.add(sp, "Center");
		this.add(panel, "South");
		// 给文本框的文档添加文档监听器
		Document doc = ta.getDocument();
		doc.addDocumentListener(new DocumentListener() {
			public void changedUpdate(DocumentEvent e) {
			}

			public void insertUpdate(DocumentEvent e) {
				setTitle(APP_NAME + "-" + file + "(*)"); // 设置修改标记
				isTextModified = true; // 设置修改标记
			}

			public void removeUpdate(DocumentEvent e) {
				setTitle(APP_NAME + "-" + file + "(*)"); // 设置修改标记
				isTextModified = true; // 设置修改标记
			}
		});
	}

	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (cmd.equals("打开文件")) { // 打开文本文件
			StringBuffer sb = new StringBuffer("");
			try {
				BufferedReader br = new BufferedReader(new FileReader("学校简介.txt"));
				String str = br.readLine();
				while (str != null) {
					sb.append(str + "\n");
					str = br.readLine();
				}
				br.close();
				ta.setText(sb.toString());
				this.setTitle(APP_NAME + "-" + file); // 窗体标题初始化
				isTextModified = false; // 重置修改标记
			} catch (Exception e1) {
				ta.setText(e1.getMessage());
			}
		} else if (cmd.equals("保存文件")) { // 保存文本
			// TODO: 保存文件的代码
			this.setTitle(APP_NAME + "-" + file); // 窗体标题重置
			isTextModified = false; // 保存后修改
		} else if (cmd.equals("复制")) {
			ta.paste();
		}
	}

	// 初始化全局字体
	private static void initGlobalFont() {
		FontUIResource fontUIResource = new FontUIResource(new Font("宋体", Font.PLAIN, 14));
		for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof FontUIResource) {
				UIManager.put(key, fontUIResource);
			}
		}
	}

	public static void main(String[] args) {
		initGlobalFont(); // 初始化全局所有字体
		JTextAreaTester frm = new JTextAreaTester();
		frm.setVisible(true);
	}
}

JAVA实现简易文本编辑器设计

 

上一篇:对于SQL Server 2008删除或压缩数据库日志的方法


下一篇:【比赛游记】CSP2021 游记