java记事本(二)

前言

使用java编写一个记事本,实现新建/保存/另存为/退出/撤销/恢复/复制/粘贴/剪切/删除/查找/转到/全选/自动换行/字体大小/字体颜色/背景颜色/状态栏/帮助/关于等功能。时间显示创建了一个内部时钟类 Clock。字体大小设置创建了一个外部类,使用package com;引用了外部类MQFontChooser。
与上一篇文章记事本(一)的区别是子菜单添加行为动作监听器,
三、特点中有说明。

一、主类UI

//package com;
import java.awt.EventQueue;  //事件调度线程
//布局
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Dimension;
//监听器
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowListener;
import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//文件流
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.GregorianCalendar;

//菜单及工具
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.undo.UndoManager;

import java.awt.Color;
import java.awt.Graphics;

//继承窗口类JFrame以及监听接口ActionListener
public class UI extends JFrame implements ActionListener{
	private static final long serialVersionUID = 88888888;//序列号,识别唯一类的号码
	private JTextArea textArea; 				//创建文本域
	private BorderLayout borderLayout;  		//创建布局
	private JPanel panel;  						//创建控制面板
	private JScrollPane scrollpanel;  			//创建滚动面板
	private JToolBar toolState;  				//创建工具栏_状态栏
	private JPopupMenu popupMenu;				//创建鼠标右键弹窗
	private JMenuBar menuBar;  					//创建菜单栏
	//创建子菜单
	private JMenuItem itemNew;					//新建
	private JMenuItem itemOpen;					//打开
	private JMenuItem itemSave;					//保存
	private JMenuItem itemSaveAs;				//另存为
	private JMenuItem itemPage;					//页面设置
	private JMenuItem itemPrint;				//打印
	private JMenuItem itemExit;					//退出
	private JMenuItem itemUndo;					//撤销
	private JMenuItem itemRedo;					//恢复
	private JMenuItem itemCut;					//剪切
	private JMenuItem itemCopy;					//复制
	private JMenuItem itemPaste;				//粘贴
	private JMenuItem itemDelete;				//删除
	private JMenuItem itemFind;					//查找
	private JMenuItem itemTurnTo;				//转到
	private JMenuItem itemSelectAll;			//全选
	private JCheckBoxMenuItem itemNextLine;		//自动换行
	private JMenuItem itemFont;					//字体大小
	private JMenuItem itemColor;				//背景颜色
	private JMenuItem itemFontColor;			//字体颜色
	private JCheckBoxMenuItem itemStatement;	//状态栏
	private JMenuItem itemSearchForHelp;		//帮助
	private JMenuItem itemAboutNote;			//关于
	//鼠标右键子菜单
	private JMenuItem item_popM_Undo;			//撤销
	private JMenuItem item_popM_Redo;			//恢复
	private JMenuItem item_popM_Copy;			//复制
	private JMenuItem item_popM_Paste;			//粘贴
	private JMenuItem item_popM_Cut;			//剪切
	private JMenuItem item_popM_Delete;			//删除
	private JMenuItem item_popM_SelectAll;		//全选
			
	int flag = 0;  								//用于判断文档保存状态
	String currentPath = null ;  				//当前文件路径
	String currentFileName = null;  			//当前文件名
	JColorChooser jcc1 = null;					//背景颜色选择器
	Color color = Color.BLACK;					//默认背景颜色
   
	//文本的行数与列数与字数
	int linenum = 1;
	int columnnum = 1;
	int length = 0;
	
	public static JLabel tool_label_time; 		//工具栏时间标签,static定义为专属这个类
   
    //获取系统剪贴板
    public Clipboard clipboard = new Clipboard("系统剪切板");
    
    //撤销管理器
    public UndoManager undoMgr = new UndoManager();
    
    //程序开始
    public static void main(String[] args) {
    	EventQueue.invokeLater(new Runnable(){
    		public void run() {
    			try {
    				UI My_UI = new UI();
    				My_UI.setVisible(true);
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	});
    }
    
    //构造函数
    public UI() {
    	//创建窗口
    	this.setTitle("liyangwei");    					//设置窗口
		this.setSize(300,400);							//设置窗口大小
		this.setLocationRelativeTo(null);				//设置窗口相对于指定组件的位置,null表示在中间
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置关闭窗口时关闭程序
    	
		textArea = new JTextArea();    					//创建文本域
		menuBar = new JMenuBar();						//创建菜单栏
		popupMenu = new JPopupMenu();					//创建鼠标右键弹窗

		//创建菜单
		JMenu Menu_File = new JMenu("文件(F)");			
		JMenu Menu_Edit = new JMenu("编辑(E)");
		JMenu Menu_Format = new JMenu("格式(O)");
		JMenu Menu_Check = new JMenu("查看(V)");
		JMenu Menu_Help = new JMenu("帮助(H)");
		//创建子菜单
		itemNew = new JMenuItem("新建(N)");
		itemOpen = new JMenuItem("打开(O)");
		itemSave = new JMenuItem("保存(S)");
		itemSaveAs = new JMenuItem("另存为(A)");
		itemPage = new JMenuItem("页面设置(U)");
		itemPrint = new JMenuItem("打印(P)");
		itemExit = new JMenuItem("退出(X)");
		itemUndo = new JMenuItem("撤销(U)");
		itemRedo = new JMenuItem("恢复(R)");
		itemCut = new JMenuItem("剪切(T)");
		itemCopy = new JMenuItem("复制(C)");
		itemPaste = new JMenuItem("粘贴(P)");
		itemDelete = new JMenuItem("删除(L)");
		itemFind = new JMenuItem("查找(F)");
		itemTurnTo = new JMenuItem("转到(G)");
		itemSelectAll = new JMenuItem("全选(A)");
		itemNextLine = new JCheckBoxMenuItem("自动换行(W)");
		itemFont = new JMenuItem("字体大小(F)");
		itemColor = new JMenuItem("背景颜色(C)");
		itemFontColor = new JMenuItem("字体颜色(I)");
		itemStatement = new JCheckBoxMenuItem("状态栏(S)");
		itemSearchForHelp = new JMenuItem("查看帮助(H)");
		itemAboutNote = new JMenuItem("关于记事本(A)");
		//创建鼠标右键子菜单
		item_popM_Undo = new JMenuItem("撤销(Z)");
		item_popM_Redo = new JMenuItem("恢复(R)");
		item_popM_Copy = new JMenuItem("复制(C)");
		item_popM_Paste = new JMenuItem("粘贴(V)");
		item_popM_Cut = new JMenuItem("剪切(X)");
		item_popM_Delete = new JMenuItem("删除(L)");
		item_popM_SelectAll = new JMenuItem("全选(A)");
	
		//添加菜单到菜单栏
		menuBar.add(Menu_File);
		menuBar.add(Menu_Edit);
		menuBar.add(Menu_Format);
		menuBar.add(Menu_Check);
		menuBar.add(Menu_Help);
		//子菜单添加到菜单
		Menu_File.add(itemNew);
		Menu_File.add(itemOpen);
		Menu_File.add(itemSave);
		Menu_File.add(itemSaveAs);
		Menu_File.add(itemPage);
		Menu_File.add(itemPrint);
		Menu_File.add(itemExit);
		Menu_Edit.add(itemUndo);
		Menu_Edit.add(itemRedo);
		Menu_Edit.add(itemCut);
		Menu_Edit.add(itemCopy);
		Menu_Edit.add(itemPaste);
		Menu_Edit.add(itemDelete);
		Menu_Edit.add(itemDelete);
		Menu_Edit.add(itemFind);
		Menu_Edit.add(itemTurnTo);
		Menu_Edit.add(itemSelectAll);
		Menu_Format.add(itemNextLine);
		Menu_Format.add(itemFont);
		Menu_Format.add(itemColor);
		Menu_Format.add(itemFontColor);
		Menu_Check.add(itemStatement);
		Menu_Help.add(itemSearchForHelp);
		Menu_Help.add(itemAboutNote);	
		popupMenu.add(item_popM_Undo);
		popupMenu.add(item_popM_Redo);
		popupMenu.add(new JSeparator());  //添加分割线
		popupMenu.add(item_popM_Copy);
		popupMenu.add(item_popM_Paste);
		popupMenu.add(item_popM_Cut);
		popupMenu.add(new JSeparator());  //添加分割线
		popupMenu.add(item_popM_Delete);
		popupMenu.add(item_popM_SelectAll);
		//子菜单添加监听器
		itemNew.addActionListener(this);
		itemOpen.addActionListener(this);
		itemSave.addActionListener(this);
		itemSaveAs.addActionListener(this);
		itemPage.addActionListener(this);
		itemPrint.addActionListener(this);
		itemExit.addActionListener(this);
		itemUndo.addActionListener(this);
		itemRedo.addActionListener(this);
		itemCut.addActionListener(this);
		itemCopy.addActionListener(this);
		itemPaste.addActionListener(this);
		itemDelete.addActionListener(this);
		itemFind.addActionListener(this);
		itemTurnTo.addActionListener(this);
		itemSelectAll.addActionListener(this);
		itemNextLine.addActionListener(this);
		itemFont.addActionListener(this);
		itemColor.addActionListener(this);
		itemFontColor.addActionListener(this);
		itemStatement.addActionListener(this);
		itemSearchForHelp.addActionListener(this);
		itemAboutNote.addActionListener(this);
		item_popM_Undo.addActionListener(this);
		item_popM_Redo.addActionListener(this);
		item_popM_Copy.addActionListener(this);
		item_popM_Paste.addActionListener(this);
		item_popM_Cut.addActionListener(this);
		item_popM_Delete.addActionListener(this);
		item_popM_SelectAll.addActionListener(this);
		
		//复制粘贴剪切撤销恢复新建打开保存快捷键
		itemCopy.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.Event.CTRL_MASK));
		itemPaste.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.Event.CTRL_MASK));
		itemCut.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.Event.CTRL_MASK));
		itemUndo.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.Event.CTRL_MASK));
		itemRedo.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.Event.CTRL_MASK));
		itemNew.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.Event.CTRL_MASK));
		itemOpen.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.Event.CTRL_MASK));
		itemSave.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.Event.CTRL_MASK));
		//添加撤销管理器
		textArea.getDocument().addUndoableEditListener(undoMgr);
		
		borderLayout = new BorderLayout();				//创建布局
		panel = new JPanel(borderLayout);				//创建控制面板
		toolState = new JToolBar();						//创建工具栏
		toolState.setSize(textArea.getSize().width, 10);//设置工具栏的大小
		toolState.setVisible(false);
		//textArea文本域设置滚动面板会有一个警告:Warning:the font "Times" is not available.
		scrollpanel = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//创建滚动面板

		//控制面板添加组件
		panel.add(scrollpanel, BorderLayout.CENTER);
		panel.add(toolState, BorderLayout.SOUTH);
		//设置菜单栏为主菜单
		this.setJMenuBar(menuBar);	
		//在窗口中添加控制面板
		this.setContentPane(panel);
		//设置窗口为可见的
		this.setVisible(true);
		//添加鼠标右键弹窗监听器
		addPopup(textArea, popupMenu);
		//判断文档是否有变化
		isChanged();
		//窗口退出时,新建过或保存过文档的退出只有两种选择
		MainFrameWindowListener();
    }
    
    /*************************************************************************/
    /*
     * 函数
     */
	/*************************************************************************/
    //添加鼠标右键弹窗监听器函数
  	private static void addPopup(JTextArea component, final JPopupMenu popup)
  	{
  		component.addMouseListener(new MouseAdapter() {
  			public void mousePressed(MouseEvent e) {
  				if(e.isPopupTrigger()) //判断用户是否要求弹出菜单
  				{
  					showMenu(e);
  				}
  			}
  			public void mouseRelease(MouseEvent e) {
  				if(e.isPopupTrigger())
  				{
  					showMenu(e);
  				}
  			}
  			public void showMenu(MouseEvent e) {
  				popup.show(e.getComponent(), e.getX(), e.getY());
  			}
  		});
  	}
	/*************************/
	//判断文档是否有变化函数
	private void isChanged()
	{
		textArea.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				Character c = e.getKeyChar();
				if(c != null && !textArea.getText().toString().equals(""))
				{
					flag = 2; //文档改变过
				}
			}
		});
	}
	/*************************/
	//新建的或保存过的退出只有两种选择
	private void MainFrameWindowListener()
	{//窗口关闭监听,当窗口关闭时,退出程序
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e)
			{
				exit();  //程序退出函数
			}
		});
	}
	/*************************/
  	//实现子菜单监听器,实现接口implements ActionListener抽象的方法
  	public void actionPerformed(ActionEvent e) {
  		if(e.getSource() == itemNew)
  		{
  			newFile();//新建
  		}
  		else if(e.getSource() == itemOpen)
  		{
  			openFile();//打开
  		}
  		else if(e.getSource() == itemSave)
  		{
  			save();//保存
  		}
  		else if(e.getSource() == itemSaveAs)
  		{
  			saveAs();//另存为
  		}
  		else if(e.getSource() == itemPage)
  		{
  			page();//页面设置
  		}
  		else if(e.getSource() == itemPrint)
  		{
  			print();//打印
  		}
  		else if(e.getSource() == itemExit)
  		{
  			exit();//退出
  		}
  		else if(e.getSource() == itemUndo || e.getSource() == item_popM_Undo)
  		{
  			undo();//撤销
  		}
  		else if(e.getSource() == itemRedo || e.getSource() == item_popM_Redo)
  		{
  			redo();//恢复
  		}
  		else if(e.getSource() == itemCut || e.getSource() == item_popM_Cut)
  		{
  			cut();//剪切
  		}
  		else if(e.getSource() == itemCopy || e.getSource() == item_popM_Copy)
  		{
  			copy();//复制
  		}
  		else if(e.getSource() == itemPaste || e.getSource() == item_popM_Paste)
  		{
  			paste();//粘贴
  		}
  		else if(e.getSource() == itemDelete || e.getSource() == item_popM_Delete)
  		{			
  			delete();//删除
  		}
  		else if(e.getSource() == itemFind)
  		{
  			find();//查找
  		}
  		else if(e.getSource() == itemTurnTo)
  		{
  			turnTo();//转到
  		}
  		else if(e.getSource() == itemSelectAll || e.getSource() == item_popM_SelectAll)
  		{  			//全选
  			textArea.selectAll();
  		}
  		else if(e.getSource() == itemNextLine)
  		{		
  			nextLine();//自动换行
  		}
  		else if(e.getSource() == itemFont)
  		{
  			fontSize();//字体大小
  		}
  		else if(e.getSource() == itemColor)
  		{
  			backColor();//背景颜色
  		}
  		else if(e.getSource() == itemFontColor)
  		{
  			fontColor();//字体颜色
  		}
  		else if(e.getSource() == itemStatement)
  		{
  			statement();//状态栏
  		}
  		else if(e.getSource() == itemSearchForHelp)
  		{				
  			help();//帮助
  		}
  		else if(e.getSource() == itemAboutNote)
  		{				
  			aboutNote();//关于
  		}
  	}
  	
	/*************************/
  	//新建
  	public void newFile()
  	{
		if(flag == 0 || flag == 1) 
		{     //刚启动记事本为0,刚新建文档为1
			return;
		}
		else if(flag == 2 && this.currentPath == null)
		{
			int result = JOptionPane.showConfirmDialog(null, "是否将更改保存到无标题", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
			if(result == JOptionPane.OK_OPTION)
			{
				//没有路径,另存为
				this.saveAs();
			}
			else if(result ==  JOptionPane.NO_OPTION) 
			{
				this.textArea.setText("");
				this.setTitle("无标题");
				flag = 1;
			}
			return;
		}
		else if(flag == 2 && this.currentPath != null)
		{
			int result = JOptionPane.showConfirmDialog(null, "是否将更改保存到无标题", this.currentPath + "?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
			if(result == JOptionPane.OK_OPTION)
			{
				//有路径直接保存
				this.save(); 
			}
			else if(result == JOptionPane.NO_OPTION)
			{
				this.textArea.setText("");
				this.setTitle("无标题");
				flag = 1;
			}  
		}
		else if(flag == 3)
		{
			this.textArea.setText("");
			this.setTitle("无标题");
		}
  	}
	/*************************/
  	//打开
  	public void openFile()
  	{
		if(flag == 2 && this.currentPath == null)
		{
			int result = JOptionPane.showConfirmDialog(null, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
			if(result == JOptionPane.OK_CANCEL_OPTION) 
			{
				this.saveAs();
			}		
		}
		else if(flag == 2 && this.currentPath == null)
		{
			int result = JOptionPane.showConfirmDialog(null, "是否将更改保存到" + this.currentPath, "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
			if(result == JOptionPane.OK_OPTION)
			{
				this.save();
			}
		}
		//打开文件选择框
		JFileChooser choose = new JFileChooser();
		//选择文件
		int result = choose.showOpenDialog(null);
		if(result == JFileChooser.APPROVE_OPTION)
		{
			File file = choose.getSelectedFile();
			currentFileName = file.getName();
			currentPath = file.getAbsolutePath();
			flag = 3;
			this.setTitle(this.currentPath);
			BufferedReader bufferedRead =  null;
			try {
				//建立文件流(字符流)
				InputStreamReader inputStream = new InputStreamReader(new FileInputStream(file), "GBK");
				bufferedRead = new BufferedReader(inputStream);  //动态绑定
				//读取内容
				StringBuffer  buf = new StringBuffer();
				String lineRead = null;
				while((lineRead = bufferedRead.readLine()) != null)
				{
					//System.getProperty("line.separator")相当于换行操作,可以在不同系统下运行换行操作
					buf.append(lineRead+System.getProperty("line.separator"));
				}
				//显示内容在文本框
				textArea.setText(buf.toString());
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			} finally {
				try {
					if(bufferedRead != null) bufferedRead.close();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		}
  	}
	/*************************/
  	//保存
  	public void save()
  	{
		if(this.currentPath == null)
		{
			this.saveAs();
			if(this.currentPath == null)
			{
				return;
			}
		}
		FileWriter fw = null;
		//保存
		try {
			fw = new FileWriter(new File(currentPath));
			fw.write(textArea.getText());
			fw.flush();  //如果写入的数据比较少,则需要刷新缓存区
			flag = 3;
			this.setTitle(this.currentPath);
		} catch (IOException e1) {
			e1.printStackTrace();
		}finally {
			try {
				if (fw != null) fw.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
  	}
	/*************************/
  	//另存为
  	public void saveAs()
  	{
		JFileChooser choose = new JFileChooser();
		//选择文件
		int result = choose.showSaveDialog(null);
		if (result == JFileChooser.APPROVE_OPTION) {
			//取得选择的文件
			File file = choose.getSelectedFile();
			FileWriter fw =null;
			//保存
			try {
				fw = new FileWriter(file);
				fw.write(textArea.getText());
				currentFileName=file.getName();
                currentPath = file.getAbsolutePath();
				fw.flush();  //如果写入的数据比较少,需要需要刷新缓存区
				this.flag = 3;
				this.setTitle(currentPath);
			}catch (IOException e1) {
				e1.printStackTrace();
			}finally {
				try {
					if(fw != null) fw.close();
				} catch (IOException e1){
					e1.printStackTrace();
				} 
			}
		}
  	}
	/*************************/	
  	//页面设置
  	public void page() 
  	{
		PageFormat pf = new PageFormat();
		PrinterJob.getPrinterJob().pageDialog(pf);
  	}
  	/*************************/
  	//打印
  	public void print()
  	{
  		
  	}
	/*************************/
  	//退出
  	public void exit()
  	{
		if(flag == 2 && currentPath == null)
		{
			//这是弹出小窗
			//刚启动记事本为0,刚新建文档为1
			int result = JOptionPane.showConfirmDialog(null, "是否将更改保存到无标题?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
			if(result == JOptionPane.OK_OPTION)
			{
				this.saveAs();
			}
			else if(result == JOptionPane.NO_OPTION)
			{
				//退出程序
				this.dispose();
			}
		}
		else if(flag == 2 && currentPath != null)
		{
			int result = JOptionPane.showConfirmDialog(null, "是否将更改保存到" + currentPath +"?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
			if(result == JOptionPane.OK_OPTION)
			{
				this.save();
			}
			else if(result == JOptionPane.NO_OPTION)
			{
				//退出程序
				this.dispose();
			}
		}
		else
		{
			int result = JOptionPane.showConfirmDialog(null, "确定关闭?", "系统提示", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
			if(result == JOptionPane.OK_OPTION)
			{
				//退出程序
				this.dispose();
			}
		}
  	}
	/*************************/
  	//撤销
  	public void undo()
  	{
		if(undoMgr.canUndo())
		{
	  		undoMgr.undo();//撤销
		}
  	}
	/*************************/
  	//恢复
  	public void redo()
  	{
		if(undoMgr.canRedo())
		{
	  		undoMgr.redo();//恢复
		}		
  	}
	/*************************/
  	//剪切
  	public void cut()
  	{
		copy();
		int start = this.textArea.getSelectionStart();  //标记开始位置
		int end = this.textArea.getSelectionEnd();  //标记结束位置
		//删除所选文段
		this.textArea.replaceRange("", start, end);
  	}
	/*************************/
  	//复制
  	public void copy()
  	{
		//拖动选取文本
		String temp = this.textArea.getSelectedText();
		//把获取的内容复制到连续字符器,这个类继承了剪贴板接口
		StringSelection text = new StringSelection(temp);
		//把内容放在剪贴板
	    clipboard.setContents(text, null);
  	}
	/*************************/
  	//粘贴
  	public void paste()
  	{
		//Transferable接口,把剪贴板的内容转换成数据
		Transferable contents = this.clipboard.getContents(this);
		//DataFalvor类判断是否能把剪贴板的内容转换成所需要数据类型
		DataFlavor flavor = DataFlavor.stringFlavor;
		//如果可以转换
		if(contents.isDataFlavorSupported(flavor));
		{
			String str;
			try {
				//开始转换
				str = (String)contents.getTransferData(flavor);
				//如果要粘贴时,鼠标已经选中了一些字符
				if(this.textArea.getSelectedText() != null)
				{
					int start = this.textArea.getSelectionStart();  //定位被选中的字符的开始位置
					int end = this.textArea.getSelectionEnd();  //定位被选中字符的开始位置
					this.textArea.replaceRange(str, start, end);  //把粘贴的内容替换成被选中的内容
				}
				else
				{
					//获取鼠标所在TextArea的位置
					int mouse = this.textArea.getCaretPosition();
					//在鼠标所在的位置粘贴内容
					this.textArea.insert(str, mouse);
				}
			} catch(UnsupportedFlavorException e) {
				e.printStackTrace();
			} catch(IOException e) {
				e.printStackTrace();
			} catch(IllegalArgumentException e) {
				e.printStackTrace();
			}
		}
  	}
	/*************************/
  	//删除
  	public void delete()
  	{
		String tem = textArea.getText().toString();
		textArea.setText(tem.substring(0, textArea.getSelectionStart()));
  	}
	/*************************/
  	//查找
  	public void find()
  	{
		final JDialog findDialog = new JDialog(this, "查找与替换", true); //“查找与替换对话框”
		Container con = findDialog.getContentPane();  //布局管理器
		con.setLayout(new FlowLayout(FlowLayout.LEFT));  //设置布局管理器
		JLabel searchContentLabel = new JLabel("查找内容(N):");
		JLabel replaceContentLabel = new JLabel("替换成为(N):");
		final JTextField findText = new JTextField(16);
		final JTextField replaceText = new JTextField(16);
		final JCheckBox matchcase = new JCheckBox("区分大小写");
		final JRadioButton up = new JRadioButton("向上(U)");
		final JRadioButton down = new JRadioButton("向下(D)");
		down.setSelected(true);  //down框默认选中
		JButton searchNext = new JButton("查找下一个(F)");
		JButton replace = new JButton("替换(R)");
		final JButton replaceAll = new JButton("全部替换(A)");
		searchNext.setPreferredSize(new Dimension(110, 22));
		replace.setPreferredSize(new Dimension(110, 22));
		replaceAll.setPreferredSize(new Dimension(110, 22));
		//“替换”按钮的事件处理
		replace.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(replaceText.getText().length() == 0 && textArea.getSelectedText() != null)
					textArea.replaceSelection("");
				if(replaceText.getText().length() > 0 && textArea.getSelectedText() != null)
					textArea.replaceSelection(replaceText.getText());	
			}
		});
		//"替换全部"按钮的事件处理
		replaceAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textArea.setCaretPosition(0);  //将光标放到编辑区开头
				int a = 0;	//要替换的文本开头
				int b = 0;  //要替换的文本结尾
				int replaceCount = 0;  //替换多少处文本
				//提示要输入查找的文本
				if(findText.getText().length() == 0)
				{
					JOptionPane.showMessageDialog(findDialog, "请填写查找内容!", "提示", JOptionPane.WARNING_MESSAGE);
					findText.requestFocus(true);
					return;
				}
				while (a > -1)  //当indexOf()函数没有找到匹配到内容时,返回-1,即结束循环
				{
					int FindStartPos = textArea.getCaretPosition(); //获取编辑区中光标的位置
					String str1, str2;
					str1 = textArea.getText();  //获取编辑区的文本
					str2 = findText.getText();  //获取查找内容框中的文本
				
					if(textArea.getSelectedText() == null) //当执行textArea.select(a, a + b)之后就选中编辑框中的文本
					{
						//从编辑区的开头开始查找(前面已经将光标放到编辑区开头)
						a = str1.indexOf(str2, FindStartPos);  //第二个参数表示从第几个位置开始向后查,str2第一个出现的位
					}
					else
					{
						//查找下一个匹配的内容
						a = str1.indexOf(str2, FindStartPos - findText.getText().length()+1);
					}
					if(a > -1)
					{
						textArea.setCaretPosition(a);  //设置编辑区中光标开始的位置
						b = findText.getText().length(); 
						textArea.select(a, a + b);  //选中textArea文本框中的指定区域
					}
					else
					{
						if(replaceCount == 0)
						{
							JOptionPane.showMessageDialog(findDialog, "查不到您查找的内容!", "记事本", JOptionPane.INFORMATION_MESSAGE);
						}
						else
						{
							JOptionPane.showMessageDialog(findDialog, "成功替换" + replaceCount +"个匹配内容!", "替换成功", JOptionPane.INFORMATION_MESSAGE);
						}
					}
					if(replaceText.getText().length() == 0 && textArea.getSelectedText() != null)
					{
						textArea.replaceSelection("");  //先选中textArea文本框中的指定区域,然后调用此方法就可以将选中的区域文字替换为“”
						replaceCount++;
					}
					if(replaceText.getText().length() > 0 && textArea.getSelectedText() != null)
					{
						textArea.replaceSelection(replaceText.getText());  先选中textArea文本框中的指定区域,然后调用此方法就可以将选中的区域文字替换为replaceText.getText()
						replaceCount++;
					}
				}  //end while
			}
		}); //"替换全部"按钮的事件处理结束 
		// "查找下一个"按钮事件处理
		searchNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int a = 0, b = 0;
				int FindStartPos = textArea.getCaretPosition();
				String str1, str2, str3, str4, strA, strB;
				str1 = textArea.getText();
				str2 = str1.toLowerCase();  //把字符串全部转换成小写,而非字母字符则不变
				str3 = findText.getText();
				str4 = str3.toLowerCase();  //把字符串全部转换成小写,而非字母字符则不变
				//“区分大小写”的CheckBox被选中
				if(matchcase.isSelected())
				{
					strA = str1;
					strB = str3;
				}
				else 
				{
					strA = str2;
					strB = str4;
				}
				if(up.isSelected())  //判断up框是否被选中
				{
					if(textArea.getSelectedText() == null)
					{
						第二个参数表示从第几个位置开始向前查,strB第一个出现的位置
						a = strA.lastIndexOf(strB, FindStartPos - 1);
					}
					else
					{
						//第二个参数表示从第几个位置开始向前查,strB第一个出现的位置
						a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1);
					}
				}
				else if(down.isSelected())  //判断down框是否被选中
				{
					if(textArea.getSelectedText() == null)  //判断textArea编辑区是否有被选中的文本
					{
						//第二个参数表示从第几个位置开始向后查,str2第一个出现的位置
						a = strA.indexOf(strB, FindStartPos);
					}
					else
					{
						//第二个参数表示从第几个位置开始向后查,str2第一个出现的位置
						a = strA.indexOf(strB, FindStartPos - findText.getText().length() + 1);
					}
				}
				if(a > -1)
				{
					textArea.setCaretPosition(a);  //设置编辑区中光标开始的位置
					b = findText.getText().length();
					textArea.select(a, a + b);  //选中textArea文本框中的指定区域
				}
				else
				{
					JOptionPane.showMessageDialog(null, "找不到您查找的内容!", "记事本", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		}); //"查找下一个"按钮事件处理结束
		
		//"取消"按钮事件处理
		JButton cancel = new JButton("取消");
		cancel.setPreferredSize(new Dimension(110, 22));
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				findDialog.dispose(); //退出窗口
			}
		});
		
		//创建"查找与替换"对话框的界面
		JPanel bottomPanel = new JPanel();  //下面板
		JPanel centerPanel = new JPanel();  //中面板
		JPanel topPanel = new JPanel();	    //上面板
		JPanel searchPanel = new JPanel();  //网格面板
		//设置网格面板
		searchPanel.setLayout(new GridLayout(2,1));
		searchPanel.add(searchNext);
		searchPanel.add(replace);
		//面板添加组件
		topPanel.add(searchContentLabel);
		topPanel.add(findText);
		topPanel.add(searchPanel);
		centerPanel.add(replaceContentLabel);
		centerPanel.add(replaceText);
		centerPanel.add(replaceAll);
		bottomPanel.add(matchcase);
		bottomPanel.add(up);
		bottomPanel.add(down);
		bottomPanel.add(cancel);
		//往面板容器中添加面板
		con.add(topPanel);
		con.add(centerPanel);
		con.add(bottomPanel);
		
		//设置"查找与替换"对话框的大小,不可变大小,位置和可见性
		findDialog.setSize(410, 210);  
		findDialog.setResizable(false);  //设置查找对话框不可以改变大小
		findDialog.setLocation(230, 280); //设置查找对话框的位置
		findDialog.setVisible(true);  //设置查找对话框为可见的
  	}
	/*************************/
  	//转到
  	public void turnTo()
  	{
		final JDialog gotoDialog = new JDialog(this, "转到下列行");
		JLabel gotoLabel = new JLabel("行数(L):");
		final JTextField linenum = new JTextField(5);
		linenum.setText("1");
		linenum.selectAll();
		JButton okButton = new JButton("确定");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int totalLine = textArea.getLineCount();
				int[] lineNumber = new int[totalLine +1];
				String s = textArea.getText();
				int pos = 0, t = 0;
				while(true) {
					pos = s.indexOf('\12', pos);
					if(pos == -1)
					{
						break;
					}
					lineNumber[t++] = pos++;
				}
				int gt = 1;
				try {
					gt = Integer.parseInt(linenum.getText());
				} catch (NumberFormatException efe) {
					JOptionPane.showMessageDialog(null, "请输入行数!", "提示", JOptionPane.WARNING_MESSAGE);
					linenum.requestFocus(true);
					return;
				}
				if(gt < 2 || gt >= totalLine)
				{
					if(gt < 2)
					{
						textArea.setCaretPosition(0);
					}
					else
					{
						textArea.setCaretPosition(s.length());
					}
				}
				else
				{
					textArea.setCaretPosition(lineNumber[gt - 2] + 1);
				}
				gotoDialog.dispose();
			}
		});
		JButton cancelButton = new JButton("取消");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gotoDialog.dispose();
			}
		});
		Container con = gotoDialog.getContentPane();
		con.setLayout(new FlowLayout());
		con.add(gotoLabel);
		con.add(linenum);
		con.add(cancelButton);
		con.add(okButton);
		gotoDialog.setSize(200, 200);
		gotoDialog.setResizable(false);//设置窗口不可变
		gotoDialog.setLocation(300, 280);
		gotoDialog.setVisible(true);
  	}
	/*************************/
  	//自动换行
  	public void nextLine()
  	{
		if(itemNextLine.isSelected())
		{
			textArea.setLineWrap(true);
		}
		else
		{
			textArea.setLineWrap(false);
		}
  	}
	/*************************/
  	//字体大小
  	public void fontSize()
  	{
  		/*
  		package com.MQFontChooser;//导入自己创建的外部类
  		MQFontChooser fontChooser = new MQFontChooser(textArea.getFont());
		fontChooser.showFontDialog(my_frame);
		Font font = fontChooser.getSelectFont();
		//将字体设置到JTextArea中
		textArea.setFont(font);
  		*/
  	}
	/*************************/
  	//背景颜色
  	public void backColor()
  	{
		jcc1 = new JColorChooser();
		JOptionPane.showMessageDialog(null, jcc1, "选择背景颜色", -1);
		color = jcc1.getColor();
		textArea.setBackground(color);
  	}
	/*************************/
  	//字体颜色
  	public void fontColor()
  	{
		// TODO Auto-generated method stub
		jcc1 = new JColorChooser();
		JOptionPane.showMessageDialog(null, jcc1, "选择字体颜色", -1);
		color = jcc1.getColor();
		textArea.setForeground(color);
  	}
	/*************************/
  	//状态栏
  	public void statement()
  	{
		if(itemStatement.isSelected()) {
			toolState.setSize(textArea.getSize().width, 10);
			
			GregorianCalendar c = new GregorianCalendar();  //获取系统时间	
			int hour = c.get(Calendar.HOUR_OF_DAY);
			int min = c.get(Calendar.MINUTE);
			int second = c.get(Calendar.SECOND);
			tool_label_time = new JLabel("  Time:" + hour + ":" + min + ":" + second);  //此标签用来显示从时钟获取的时间
			
			JLabel tool_label1 = new JLabel("  第" + linenum + "行  ");
			JLabel tool_label2 = new JLabel("  第"+ columnnum + "列  ");
			JLabel tool_label3 = new JLabel("  共" + length + "字  ");
			
			toolState.add(tool_label1);
			toolState.addSeparator();  //添加分割线
			toolState.add(tool_label2);
			toolState.addSeparator();  //添加分割线
			toolState.add(tool_label3);
			toolState.addSeparator();  //添加分割线
			toolState.add(tool_label_time);
			textArea.addCaretListener(new CaretListener() {  //文本改变监听器
				public void caretUpdate(CaretEvent e) {
					JTextArea editArea = (JTextArea)e.getSource();
					try {
						int caretpos = editArea.getCaretPosition();
						linenum = editArea.getLineOfOffset(caretpos);
						columnnum = caretpos - textArea.getLineStartOffset(linenum);
						linenum += 1;
						tool_label1.setText("  第" + linenum + "行  ");
						tool_label2.setText("  第"+ columnnum + "列  ");
						length = textArea.getText().toString().length();
						tool_label3.setText("  一共" + length + " 字 ");
					} catch (Exception ex) {}
				}
			});
			toolState.setVisible(true);  //当选择状态栏时工具栏显示
			//创建时钟实例(自己创建的一个内部时钟类)
			Clock clock = new Clock();
			//时钟线程开始
			clock.start();
		}
		else {
			toolState.setVisible(false);  //当不选择状态栏时工具栏不显示
		}	
  	}
  	//帮助
	/*************************/
  	public void help()
  	{
		JOptionPane.showMessageDialog(this, "liyangwei", "帮助", 1);
  	}
	/*************************/
  	//关于
  	public void aboutNote()
  	{
		JOptionPane.showMessageDialog(this, "记事本1.0", "关于", 1);
  	}
  	
  	
  	//===========================================================================================================
	/*
	 * 创建一个时钟类(一个内部类)
	 */
	class Clock extends Thread{
		public void run() {
			while(true) {
				/*
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-DD 'at' HH:mm:ss z");  //显示时间的格式import java.text.SimpleDateFormat;
				Date date = new Date(System.currentTimeMillis());  //获取时间import java.util.Date;
				System.println("Time:"+formatter.format(date) );
				*/
				GregorianCalendar time = new GregorianCalendar();
				int hour = time.get(Calendar.HOUR_OF_DAY);
				int min = time.get(Calendar.MINUTE);
				int second = time.get(Calendar.SECOND);
				UI.tool_label_time.setText("  Time:" + hour + ":" + min + ":" + second);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException exception) {
					//发生错误时的信息提示
				}
			}
		}
	}
  
  	
}


二、外部字体设置类MQFontChooser

package com;//引用com包中的类

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;

public class MQFontChooser extends JDialog {
	
	private static final long serialVersionUID = 12345678;  //序列号
	
	public static final int CANCEL_OPTION = 0;  //选择取消按钮的返回值
	public static final int APPROVE_OPTION = 1;  //选择确定按钮的返回值
	private static final String CHINA_STRING = "嘻嘻";   //中文预览字符串
	private static final String ENGLISH_STRING = "licanbin";  //英文预览的字符串
	private static final String NUMBER_STRING = "0123456789";  //数字预览的字符串
	private Font font = null; //预设字体,也是将来要返回的字体
	private Box box = null;  //字体选择器组件容器
	private JTextField fontText = null;  //字体文本框
	private JTextField styleText = null; //样式文本框
	private JTextField sizeText = null; //文字大小文本框
	private JTextField previewText = null; //预览文本框
	private JRadioButton chinaButton = null; //中文预览
	private JRadioButton englishButton = null; //英文预览
	private JRadioButton numberButton = null; //数字预览
	private JList fontList = null; //字体选择框
	private JList styleList = null; //样式选择器
	private JList sizeList = null; //文字大小选择器
	private JButton approveButton = null;  //确定按钮
	private JButton cancelButton = null;  //取消按钮
	private String [] fontArray = null; //所有字体
	private String [] styleArray = {"常规", "粗体", "斜体", "粗斜体"};  //所有样式
	private String [] sizeArray = {"8", "9", "10", "11", "12", "14", "16", "18", "20", "22","24", "26", "28", "36", "48", "初号", "小初", "一号", "小一", "二号","小二","三号", "小三", "四号", "小四", "五号","小五","六号", "小六", "七号", "八号"}; //所有预设字体大小
	private int [] sizeIntArray = {8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 42, 36, 26, 24, 22, 18, 16, 15, 14, 12, 10, 9, 8, 7, 6, 5};
	private int returnValue = CANCEL_OPTION;  //返回的数值,默认取消
	
	//构造一个字体选择器
	public MQFontChooser() {
		new Font("宋体", Font.PLAIN, 12);
	}
	//使用给定的预设字体构造一个字体选择器
	public MQFontChooser(Font font) {
		setTitle("字体选择器");
		this.font = font;
		//初始化UI组件
		init();
		//添加监听器
		addListener();
		//按照预设字体显示
		setup();
		//基本设置
		setModal(true);
		setResizable(false);
		//自适应大小
		pack();
	}
	//初始化组件
	private void init() {
		//获取系统字体
		GraphicsEnvironment eq = GraphicsEnvironment.getLocalGraphicsEnvironment();
		fontArray = eq.getAvailableFontFamilyNames();
		//主容器
		box = Box.createVerticalBox();
		box.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
		fontText = new JTextField();
		fontText.setEditable(false);
		fontText.setBackground(Color.WHITE);
		styleText = new JTextField();
		styleText.setEditable(false);
		styleText.setBackground(Color.WHITE);
		sizeText = new JTextField("12");
		//给文字大小文本框使用的Document文档,制定了一些输入字符的规则
		Document doc = new PlainDocument() {
			public void insertString(int offs, String str, AttributeSet a) throws BadLocationException{
				if(str == null) {
					return;
				}
				if(getLength() >= 3) {
					return;
				}
				if(!str.matches("[0-9]+") && !str.equals("初号") && !str.equals("小初")&& !str.equals("一号") && !str.equals("小一") && !str.equals("二号") && !str.equals("小二")&& !str.equals("三号") && !str.equals("小三")&& !str.equals("四号") && !str.equals("小四")&& !str.equals("五号") && !str.equals("小五")&& !str.equals("六号") && !str.equals("小六")&& !str.equals("七号") && !str.equals("八号")) {
					return;
				}
				super.insertString(offs, str, a);
				sizeList.setSelectedValue(sizeText.getText(), true);
			}
		};
		sizeText.setDocument(doc);
		previewText = new JTextField(20);
		previewText.setHorizontalAlignment(JTextField.CENTER);
		previewText.setEditable(false);
		previewText.setBackground(Color.WHITE);
		chinaButton = new JRadioButton("中文预览", true);
		englishButton = new JRadioButton("英文预览");
		numberButton = new JRadioButton("数字预览");
		ButtonGroup bg = new ButtonGroup();
		bg.add(chinaButton);
		bg.add(englishButton);
		bg.add(numberButton);
		fontList = new JList(fontArray);
		styleList = new JList(styleArray);
		sizeList = new JList(sizeArray);
		approveButton = new JButton("确定");
		cancelButton = new JButton("取消");
		Box box1 = Box.createHorizontalBox();
		JLabel l1 = new JLabel("字体:");
		JLabel l2 = new JLabel("字形:");
		JLabel l3 = new JLabel("大小:");
		l1.setPreferredSize(new Dimension(165, 14));
		l1.setMaximumSize(new Dimension(165, 14));
		l1.setMinimumSize(new Dimension(165, 14));
		l2.setPreferredSize(new Dimension(95, 14));
		l2.setMaximumSize(new Dimension(95, 14));
		l2.setMinimumSize(new Dimension(95, 14));
		l3.setPreferredSize(new Dimension(80, 14));
		l3.setMaximumSize(new Dimension(80, 14));
		l3.setMinimumSize(new Dimension(80, 14));
		box1.add(l1);
		box1.add(l2);
		box1.add(l3);
		Box box2 = Box.createHorizontalBox();
		fontText.setPreferredSize(new Dimension(160, 20));
		fontText.setMaximumSize(new Dimension(160, 20));
		fontText.setMinimumSize(new Dimension(160, 20));
		box2.add(fontText);
		box2.add(Box.createHorizontalStrut(5));
		styleText.setPreferredSize(new Dimension(90, 20));
		styleText.setMaximumSize(new Dimension(90, 20));
		styleText.setMinimumSize(new Dimension(90, 20));
		box2.add(styleText);
		box2.add(Box.createHorizontalStrut(5));
		sizeText.setPreferredSize(new Dimension(80, 20));
		sizeText.setMaximumSize(new Dimension(80, 20));
		sizeText.setMinimumSize(new Dimension(80, 20));
		box2.add(sizeText);
		Box box3 = Box.createHorizontalBox();
		JScrollPane sp1 = new JScrollPane(fontList);
		sp1.setPreferredSize(new Dimension(160, 100));
		sp1.setMaximumSize(new Dimension(160, 100));
		sp1.setMinimumSize(new Dimension(160, 100));
		box3.add(sp1);
		box3.add(Box.createHorizontalStrut(5));
		JScrollPane sp2 = new JScrollPane(styleList);
		sp2.setPreferredSize(new Dimension(90, 100));
		sp2.setMaximumSize(new Dimension(90, 100));
		sp2.setMinimumSize(new Dimension(90, 100));
		box3.add(sp2);
		box3.add(Box.createHorizontalStrut(5));
		JScrollPane sp3 = new JScrollPane(sizeList);
		sp3.setPreferredSize(new Dimension(80, 100));
		sp3.setMaximumSize(new Dimension(80, 100));
		sp3.setMinimumSize(new Dimension(80, 100));
		box3.add(sp3);
		Box box4 = Box.createHorizontalBox();
		Box box5 = Box.createVerticalBox();
		JPanel box6 = new JPanel(new BorderLayout());
		box5.setBorder(BorderFactory.createTitledBorder("字符集"));
		box6.setBorder(BorderFactory.createTitledBorder("示例"));
		box5.add(chinaButton);
		box5.add(englishButton);
		box5.add(numberButton);
		box5.setPreferredSize(new Dimension(90, 95));
		box5.setMaximumSize(new Dimension(90, 95));
		box5.setMinimumSize(new Dimension(90, 95));
		box6.add(previewText);
		box6.setPreferredSize(new Dimension(90, 95));
		box6.setMaximumSize(new Dimension(90, 95));
		box6.setMinimumSize(new Dimension(90, 95));
		box4.add(box5);
		box4.add(Box.createHorizontalStrut(4));
		box4.add(box6);
		Box box7 = Box.createHorizontalBox();
		box7.add(Box.createHorizontalGlue());
		box7.add(approveButton);
		box7.add(Box.createHorizontalStrut(5));
		box7.add(cancelButton);
		box.add(box1);
		box.add(box2);
		box.add(box3);
		box.add(Box.createVerticalStrut(5));
		box.add(box4);
		box.add(Box.createVerticalStrut(5));
		box.add(box7);
		getContentPane().add(box);
	}
	//按照预设字体显示
	private void setup() {
		String fontName = font.getFamily();
		int fontStyle = font.getStyle();
		int fontSize = font.getSize();
		//如果预设的文字大小在选择列表中,则通过选择列表中的某项进行设值,否则直接将预设文字大小写入文本框
		boolean b = false;
		for(int i = 0; i < sizeArray.length; i++) {
			if(sizeArray[i].equals(String.valueOf(fontSize))) {
				b = true;
				break;
			}
		}
		if(b) {
			//选择文字大小列表中的某项
			sizeList.setSelectedValue(String.valueOf(fontSize), true);
		}
		else
		{
			sizeText.setText(String.valueOf(fontSize));
		}
		//选择字体列表中的某项
		fontList.setSelectedValue(fontName, true);
		//选择样式列表中的某项
		styleList.setSelectedIndex(fontStyle);
		//预览默认显示中文字符
		chinaButton.doClick();
		//显示预览
		setPreview();
	}
	//添加所需的事件监听器
	private void addListener() {
		sizeText.addFocusListener(new FocusListener() {
			public void focusLost(FocusEvent e) {
				setPreview();
			}
			public void focusGained(FocusEvent e) {
				sizeText.selectAll();
			}
		});
		//字体列表发生选择事件的监听器
		fontList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				if(!e.getValueIsAdjusting()) {
					fontText.setText(String.valueOf(fontList.getSelectedValue()));
					//设置预览
					setPreview();
				}
			}
		});
		styleList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				if(!e.getValueIsAdjusting()) {
					styleText.setText(String.valueOf(styleList.getSelectedValue()));
					setPreview();
				}
			}
		});
		sizeList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				if(!e.getValueIsAdjusting()) {
					if(!sizeText.isFocusOwner()) {
						sizeText.setText(String.valueOf(sizeList.getSelectedValue()));
					}
					//设置预览
					setPreview();
				}
			}
		});
		//编码监听器
		EncodeAction ea = new EncodeAction();
		chinaButton.addActionListener(ea);
		englishButton.addActionListener(ea);
		numberButton.addActionListener(ea);
		//确定按钮的事件监听
		approveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			//组合字体
			font = groupFont();
			//设置返回值
			returnValue = APPROVE_OPTION;
			//关闭窗口
			disposeDialog();
			}
		});
		//取消按钮事件监听
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				disposeDialog();
			}
		});
	}
	
	//显示字体选择器
	public final int showFontDialog(JFrame owner) {
		setLocationRelativeTo(owner);
		setVisible(true);
		return returnValue;
	}
	//返回选择的字体对象
	public final Font getSelectFont() {
		return font;
	}
	//关闭窗口
	private void disposeDialog() {
		MQFontChooser.this.removeAll();
		MQFontChooser.this.dispose();
	}
	//显示错误信息
	private void showErrorDialog(String errorMessage) {
		JOptionPane.showMessageDialog(this, errorMessage, "错误", JOptionPane.ERROR_MESSAGE);
	}
	//设置预览
	private void setPreview() {
		Font f = groupFont();
		previewText.setFont(f);
	}
	//按照选择组合字体
	private Font groupFont() {
		String fontName = fontText.getText();
		int fontStyle = styleList.getSelectedIndex();
		String sizeStr = sizeText.getText().trim();
		//如果没有输入
		if(sizeStr.length() == 0) {
			showErrorDialog("字体(大小)必须是有效“数值!");
			return null;
		}
		int fontSize = 0;
		//通过循环对比文字大小输入是否在现有列表内
		for(int i = 0; i < sizeArray.length; i++) {
			if(sizeStr.equals(sizeArray[i])) {
				fontSize = sizeIntArray[i];
				break;
			}
		}
		//没有在列表内
		if(fontSize == 0) {
			try {
				fontSize = Integer.parseInt(sizeStr);
				if(fontSize < 1) {
					showErrorDialog("字体(大小)必须是有效数值!");
					return null;
				}
			} catch (NumberFormatException nfe) {
				showErrorDialog("字体(大小)必须是有效数值!");
				return null;
			}
		}
		return new Font(fontName, fontStyle, fontSize);
	}
	//编码选择事件的监听动作
	class EncodeAction implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(chinaButton)) {
				previewText.setText(CHINA_STRING);
			} else if(e.getSource().equals(englishButton)) {
				previewText.setText(ENGLISH_STRING);
			} else {
				previewText.setText(NUMBER_STRING);
			}
		}
	}
}

三、特点

1.行为动作监听直接添加到子菜单。
//子菜单添加监听器
//继承窗口类JFrame以及监听接口ActionListener
public class UI extends JFrame implements ActionListener{ 
	public UI(){
		//子菜单添加监听器
		itemNew.addActionListener(this);
		itemOpen.addActionListener(this);
		itemSave.addActionListener(this);
		itemSaveAs.addActionListener(this);
	}
	 	//实现子菜单监听器,实现接口implements ActionListener抽象的方法
  	public void actionPerformed(ActionEvent e) {
  		if(e.getSource() == itemNew)
  		{
  			newFile();//新建
  		}
  		else if(e.getSource() == itemOpen)
  		{
  			openFile();//打开
  		}
  		else if(e.getSource() == itemSave)
  		{
  			save();//保存
  		}
  		else if(e.getSource() == itemSaveAs)
  		{
  			saveAs();//另存为
  		}
  	}
  	public void newFile(){
  	}
  	public void openFile(){
  	}
  	public void save(){
  	}
  	public void saveAs(){
  	}
}
2.创建了一个内部时钟类Clock
3.引用了一个外部字体设置类MQFontChooser

参考文献

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值