⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notepad4.java

📁 一个文本编辑器,用JAVA程序编写的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.datatransfer.*;
import java.util.List;
import java.util.regex.*;

public class Notepad4 extends JFrame implements ActionListener, DocumentListener {
	JMenu mEdit, mHelp;
	//保存,读入,退出按钮
	JButton FSave, FRead, FExit;
	
	// ---------------统计菜单
	JMenuItem mEdit_char, mEdit_num, mEdit_blank, mEdit_string, mEdit_delstr;

	// ---------------帮助菜单
	JMenuItem mHelp_HelpTopics, mHelp_About;
	
	// 文本编辑区域
	static JTextArea Text;
	// 状态栏标签
	JLabel statusLabel1, statusLabel2, statusLabel3;
	
	// ----------------其它变量
	boolean isNewFile = true; // 是否新文件(未保存过的)
	File currentFile; // 当前文件名
	String oldValue; // 存放编辑区原来的内容,用于比较文本是否有改动
	JButton fontOkButton; // 字体设置里的"确定"按钮
	// ----------------设置编辑区默认字体
	protected Font defaultFont = new Font("宋体", Font.PLAIN, 12);
	GregorianCalendar time = new GregorianCalendar();
	int hour = time.get(Calendar.HOUR_OF_DAY);
	int min = time.get(Calendar.MINUTE);
	int second = time.get(Calendar.SECOND);
	File saveFileName = null, fileName = null;
	
	//统计数目
	JLabel sample;
	String s = new String("统计结果:");
	String te;
	int nn=0, nc=0, nb=0;

	public Notepad4() {
		super("文本编辑器");
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		Container container = getContentPane();
		JScrollPane scroll = new JScrollPane(Text);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		Text.setWrapStyleWord(true); // 设置单词在一行不足容纳时换行
		Text.setLineWrap(true);
		Text.setFont(defaultFont); // 设置编辑区默认字体
		Text.setBackground(Color.white); // 设置编辑区默认背景色
		Text.setForeground(Color.black); // 设置编辑区默认前景色
		oldValue = Text.getText(); // 获取原文本编辑区的内容
		// --------------------------编辑区注册事件监听
		Text.getDocument().addDocumentListener(this); // 添加负责通知任何更改的文档侦听器
		JMenuBar MenuBar = new JMenuBar();
		mEdit = new JMenu("统计", true);
		mHelp = new JMenu("帮助", true);
		
		FSave = new JButton("保存");
		FRead = new JButton("读入");
		FExit = new JButton("退出");
		
		MenuBar.add(FSave);
		MenuBar.add(FRead);
		MenuBar.add(FExit);
		MenuBar.add(mEdit);
		MenuBar.add(mHelp);
	
		//保存
		FSave.addActionListener(this);
		//读入
		FRead.addActionListener(this);
		//退出
		FExit.addActionListener(this);
		
		//---------------统计菜单
		mEdit_char = new JMenuItem("字母");
		mEdit_num = new JMenuItem("数字");
		mEdit_blank = new JMenuItem("空格");
		mEdit_string = new JMenuItem("字符串");
		mEdit_delstr = new JMenuItem("删除串");
		mEdit_char.addActionListener(this); // 注册事件监听
		mEdit_num.addActionListener(this);
		mEdit_blank.addActionListener(this);
		mEdit_string.addActionListener(this);
		mEdit_delstr.addActionListener(this);
		mEdit.add(mEdit_char);
		mEdit.add(mEdit_num);
		mEdit.add(mEdit_blank);
		mEdit.add(mEdit_string);
		mEdit.addSeparator();
		mEdit.add(mEdit_delstr);
		
		
		//---------------帮助菜单
		mHelp_HelpTopics = new JMenuItem("帮助");
		mHelp_About = new JMenuItem("关于");
		mHelp_HelpTopics.addActionListener(this);
		mHelp_About.addActionListener(this);
		mHelp.add(mHelp_HelpTopics);
		mHelp.addSeparator(); // 添加分割线
		mHelp.add(mHelp_About);

		// ------------------------------------改变标题栏窗口左侧默认图标
		this.setJMenuBar(MenuBar); // 向窗口添加菜单条
		container.add(scroll, BorderLayout.CENTER); // 向容器添加文本编辑区
		this.pack();
		this.setSize(800, 800);
		this.setVisible(true);
		Text.requestFocus();
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				checkText();
			}
		});
	}

	public void checkText() {
		Text.requestFocus();
		String currentValue = Text.getText();
		boolean isTextChange = (currentValue.equals(oldValue)) ? false : true;
		if (isTextChange) {

			int saveChoose = JOptionPane.showConfirmDialog(this, "您的文件尚未保存。是否保存?", "提示", JOptionPane.YES_NO_CANCEL_OPTION);

			if (saveChoose == JOptionPane.YES_OPTION) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				fileChooser.setApproveButtonText("确定");
				fileChooser.setDialogTitle("另存为");

				int result = fileChooser.showSaveDialog(this);

				if (result == JFileChooser.CANCEL_OPTION) {
					statusLabel1.setText("您没有选择任何文件");
					return;
				}

				saveFileName = fileChooser.getSelectedFile();

				if (saveFileName == null || saveFileName.getName().equals(""))
					JOptionPane.showMessageDialog(this, "不合法的文件名", "不合法的文件名", JOptionPane.ERROR_MESSAGE);
				else {
					saveFile();
					Text.setText("");
					this.setTitle("新建文本");
					statusLabel1.setText(" 新建文本");
				}
			}

			else if (saveChoose == JOptionPane.NO_OPTION) {
				System.exit(0);
			}

			else if (saveChoose == JOptionPane.CANCEL_OPTION) {
				Text.requestFocus();
			}
		}

		else if (!isTextChange) {
			System.exit(0);
		}
	}

	public void saveFile() {
		try {
			FileWriter fw = new FileWriter(saveFileName);
			fw.write(Text.getText());
			fw.close();
		} catch (Exception e) {
		}
	}
	
	
	//myCount()
	public void myCount() {
		final JDialog countDialog = new JDialog(this, "统计", true);
		Container con = countDialog.getContentPane();
		con.setLayout(new FlowLayout(FlowLayout.LEFT));
		JButton num = new JButton("数字");
		JButton cha = new JButton("字母");
		JButton bla = new JButton("空格");
		JLabel str = new JLabel("请输入字符串:");
		final JTextField Cstr = new JTextField(22);
		JButton su = new JButton("确定");
		num.setPreferredSize(new Dimension(110, 22));
		cha.setPreferredSize(new Dimension(110, 22));
		str.setPreferredSize(new Dimension(110, 22));
		
		sample = new JLabel("统计结果:");
		sample.setHorizontalAlignment(SwingConstants.CENTER);
		sample.setPreferredSize(new Dimension(150, 31));
		
		//统计
		Text.setCaretPosition(0); // 将光标放到编辑区开头
		te = Text.getText();
		char c;
		nn=0;
		nc =0;
		nb=0;
		for(int i=0; i<te.length(); i++){
			c = te.charAt(i);
			if(c == ' ') nb++;
			else if(c >'0' && c <= '9') nn++;
			else if((c > 'a' && c < 'z') || (c > 'A' && c > 'Z')) nc++;
		}
		
		//数字
		num.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String snn = String.valueOf(nn);
				snn = s + snn;
				sample.setText(snn);
			}
		});
		
		//字母
		cha.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String snc = String.valueOf(nc);
				snc = s + snc;
				sample.setText(snc);
			}
		});
		
		//空格
		bla.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String snb = String.valueOf(nb);
				snb = s + snb;
				sample.setText(snb);
			}
		});
		
		//统计字符串
		su.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Text.setCaretPosition(0); // 将光标放到编辑区开头
				te = Text.getText();
				String de = Cstr.getText();
				StringTokenizer st = new StringTokenizer(te, de);
				String sns = String.valueOf(st.countTokens());
				sns = s + sns;
				sample.setText(sns);
			}
		});
		
		// "取消"按钮及事件处理
		JButton cancel = new JButton("取消");
		cancel.setPreferredSize(new Dimension(110, 22));
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				countDialog.dispose();
			}
		});

		// 创建"统计"对话框的界面
		JPanel bottomPanel = new JPanel();
		JPanel centerPanel = new JPanel();
		JPanel topPanel = new JPanel();
		JPanel anPanel = new JPanel();
		anPanel.setLayout(new GridLayout(1, 2));
		anPanel.add(sample);

		JPanel direction = new JPanel();
		JPanel cPanel = new JPanel();
		cPanel.setLayout(new GridLayout(2, 1));
		cPanel.add(num);
		cPanel.add(cha);
		cPanel.add(bla);
		
		topPanel.add(cPanel);
		
		centerPanel.add(str);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -