废话不多说直接上代码
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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)