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

📄 notedemo.java

📁 这是一个自编的记事本程序
💻 JAVA
字号:
package cn.com.csuinfosoft;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;

import cn.com.csuinfosoft.util.IConstants;
import cn.com.csuinfosoft.util.LogWriter;
/**
 * 记事本类
 * @author 
 * 
 */

public class NoteDemo extends JFrame {

	private static final long serialVersionUID = 1L;

//	/**
//	 * 文本编辑器的主窗口
//	 */	
//	private JFrame frame = null;
	
	/**
	 * 文件内容显示域
	 */
	private JTextArea fileTextArea = null;
	
	/**
	 * 菜单条
	 */
	private JMenuBar jmBar = null;
	
	/**
	 * 工具条
	 */
	private JToolBar toolBar = null;
	
	/**
	 * 键盘事件触发操作
	 */
	private KeyListener textAreaListener = null;
	
	/**
	 * 记事本动作监听事件
	 */
	private ActionListener actionListener = null;
	
	private PrintWriter pw;
	private LogWriter logWriter;
	public NoteDemo() {
		getFrame(IConstants.FRAME_TITLE, 400, 600, true);
	}
	
	/**
	 * 创建GUI运用
	 */
	private static void createAndShowGUI() {
		JFrame frame = new NoteDemo();
	}
	
	/**
	 * 创建GUI视图界面方法
	 */
	private void getFrame(String title, int width, int length, boolean argVisible) {
	      pw = new PrintWriter(System.err, true);
	      logWriter = new LogWriter("NoteDemo", LogWriter.INFO, pw);
	      Properties dbProps = new Properties();
	      try
	      {
		     InputStream is = new  FileInputStream("io.properties");//getClass().getResourceAsStream("io.properties");
	         dbProps.load(is);
	      }
	      catch (Exception e)
	      {
	         logWriter.log("Can't read the properties file. " +
	            "Make sure io.properties is in the CLASSPATH",
	            LogWriter.ERROR);
	         return;
	      }
	      String logFile = dbProps.getProperty("logfile");
	      if (logFile != null)
	      {
	         try
	         {
	            pw = new PrintWriter(new FileWriter(logFile, true), true);
	            System.out.println("message");
	            logWriter.setPrintWriter(pw);
	    		Calendar cal = Calendar.getInstance();
	    		SimpleDateFormat fromatter = new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");
	    		String strCurrTime = fromatter.format(cal.getTime());
	            logWriter.log(strCurrTime+"开始记录日志",LogWriter.INFO);
	         }
	         catch (IOException e)
	         {
	            logWriter.log("Can't open the log file: " + logFile +
	               ". Using System.err instead", LogWriter.ERROR);
	         }
	      }
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		initFrameInfo();
		this.setTitle(title);
		this.setSize(width, length);
		this.setVisible(argVisible);
		this.setLocation(200,400);
	}
	
	public void paint(Graphics g) {
		super.paint(g);
	}

	/**
	 * 初始化窗口信息
	 */
	private void initFrameInfo() {
		fileTextArea = new JTextArea();
		this.getContentPane().add(new JScrollPane(fileTextArea));
		//创建和设置监听对象
		actionListener = new NoteActionListener(fileTextArea, this,pw,logWriter);
		textAreaListener = new NoteKeyActionListener();
		fileTextArea.addKeyListener(textAreaListener);
		
		jmBar = new JMenuBar();
		jmBar.add(buildFileMenu());
		this.setJMenuBar(jmBar);
		this.buildToolBar();
		this.getContentPane().add(toolBar,BorderLayout.NORTH);
	}
	
	/**
	 * 生成文件列表菜单
	 * @return
	 */
	private JMenu buildFileMenu() {
		JMenu thefile = new JMenu(IConstants.FILE_MENU);
		thefile.setMnemonic('F');
		thefile.add(buildMenuItem("icons/new24.gif", IConstants.FILE_NEW, 'N'));
		thefile.add(buildMenuItem("icons/open24.gif", IConstants.FILE_OPEN, 'O'));
		thefile.add(buildMenuItem("icons/close24.gif", IConstants.FILE_CLOSE, 'C'));
		thefile.add(buildMenuItem("icons/export24.gif", IConstants.FILE_SAVE, 'S'));
		thefile.addSeparator();
		thefile.add(buildMenuItem("icons/exit24.gif", IConstants.FILE_QUIT, 'E'));
		return thefile;
	}
	
	/**
	 * 构造一个菜单列表选项
	 * @param argIcon String
	 * @param argActionCommand String
	 * @param mnemonicChar char
	 * @return JMenuItem
	 */
	private JMenuItem buildMenuItem(String argIcon, String argActionCommand, char mnemonicChar) {
		JMenuItem menuItem = new JMenuItem(argActionCommand,  new ImageIcon(argIcon));
		//设置快捷键
		menuItem.setMnemonic(mnemonicChar);
		menuItem.setAccelerator( KeyStroke.getKeyStroke(mnemonicChar, java.awt.Event.CTRL_MASK, false));
		menuItem.addActionListener(actionListener);	//将"Exit"选项设置到监听器中
		return menuItem;
	}
	
	/**
	 * 创建工具栏
	 */
	private void buildToolBar() {
		toolBar = new JToolBar();
		toolBar.setFloatable(true);
		//添加按钮
		toolBar.add(buildToolButton("icons/new24.gif", IConstants.FILE_NEW));
		//打开按钮
		toolBar.add(buildToolButton("icons/open24.gif",IConstants.FILE_OPEN)); 
		//保存按钮
		toolBar.add(buildToolButton("icons/import24.gif",IConstants.FILE_SAVE));
		//关闭按钮
		toolBar.add(buildToolButton("icons/close24.gif",IConstants.FILE_CLOSE));
		//放置分割符
		toolBar.addSeparator();
		
		//添加粗体设置按钮
		toolBar.add(buildToolButton("icons/bold24.gif", IConstants.FONT_BOLD));
		//添加斜体设置按钮
		toolBar.add(buildToolButton("icons/italic24.gif", IConstants.FONT_ITALIC));
		//添加下画线设置按钮
		toolBar.add(buildToolButton("icons/underline24.gif", IConstants.FONT_UNDERLINE));		
	}
	
	/**
	 * 创建工具栏上的按钮
	 */
	private JButton buildToolButton(String icon, String argActionCommand) {
		JButton jb = new JButton(new ImageIcon(icon));//"icons/new24.gif"));
		jb.setActionCommand(argActionCommand);
		jb.setToolTipText(argActionCommand);
		jb.addActionListener(actionListener);
		return jb;
	}
	
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}
}

⌨️ 快捷键说明

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