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

📄 editordemo.java

📁 java写的,自己编写的记事本程序(个人认为功能还是挺全的哦) 有需要的朋友可以下来看看!!!
💻 JAVA
字号:
package 记事本;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Date;

import javax.swing.*;
import javax.swing.text.*;

//简单的文本编辑器

public class EditorDemo extends JFrame {
	JMenuItem jnew,jopen,jsave,jcut,jcopy,jpaste,jabout,jexit,jlook;
	JTextArea textPane = new JTextArea(); //文本窗格,编辑窗口
	JLabel statusBar = new JLabel(); //状态栏
	JFileChooser filechooser = new JFileChooser(); //文件选择器
	
	JComboBox jc1,jc2,jc3,jc4,jc5;
	String ziti="宋体";
	int zihao=14;
	int style=Font.PLAIN;
	String s2[]={"Plain","Bold","Italic","Bold+Italic"};
	String s1[]={"8","10","12","14","16","18","20","22","24"};
	String s3[]={"宋体","黑体","隶书","楷体_GB2312"};
	String s4[]={"Black","Green","Cyan","red","LightGray","Orange","Pink","Yellow"};
	String s5[]={"Black","Green","Cyan","red","LightGray","Orange","Pink","Yellow"};

	public EditorDemo() { //构造函数
		super("简单的文本编辑器");  //调用父类构造函数
		

		Action[] actions =  //Action数组,各种操作命令
			{
				new NewAction(),
				new OpenAction(),
				new SaveAction(),
				new CutAction(),
				new CopyAction(),
				new PasteAction(),
				new AboutAction(),
				new ExitAction()
			};
		jnew=new JMenuItem(actions[0]);
		jopen=new JMenuItem(actions[1]);
		jsave=new JMenuItem(actions[2]);//设置快捷键
		jcut=new JMenuItem(actions[3]);
		jcopy=new JMenuItem(actions[4]);
		jpaste=new JMenuItem(actions[5]);
		jabout=new JMenuItem(actions[6]);
		jexit=new JMenuItem(actions[7]);
		
		jlook=new JMenuItem("状态栏(S)");
		jlook.setEnabled(false);
		
		jnew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
		jopen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
		jsave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
		jcut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,InputEvent.CTRL_MASK));
		jcopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
		jpaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
		jabout.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK));
		jexit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
		

		setJMenuBar(createJMenuBar(actions));  //设置菜单栏
		Container container = getContentPane(); //得到容器
		container.add(createJToolBar(actions), BorderLayout.NORTH); //增加工具栏
		container.add(textPane, BorderLayout.CENTER); //增加文本窗格
		container.add(statusBar, BorderLayout.SOUTH); //增加状态栏

		setSize(600, 400); //设置窗口尺寸
		setVisible(true);  //设置窗口可视
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //关闭窗口时退出程序
	}

	private JMenuBar createJMenuBar(Action[] actions) {  //创建菜单栏
		JMenuBar menubar = new JMenuBar(); //实例化菜单栏
		JMenu menuFile = new JMenu("文件(F)"); //实例化菜单
		JMenu menuEdit = new JMenu("编辑(E)");
		JMenu menuAbout = new JMenu("帮助(H)");
		JMenu menuformat =new JMenu("格式(O)");
		JMenu menulook =new JMenu("查看(V)");
		menuFile.setMnemonic('f');
		menuFile.setMnemonic('F');
		menuEdit.setMnemonic('e');
		menuEdit.setMnemonic('E');
		menuAbout.setMnemonic('H');
		menuAbout.setMnemonic('h');
		menuformat.setMnemonic('O');
		menuformat.setMnemonic('o');
		menulook.setMnemonic('V');
		menulook.setMnemonic('v');
		menulook.add(jlook);
		 //menuFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));////////设置快捷键
		menuFile.add(jnew); //增加新菜单项
		menuFile.add(jopen);
		menuFile.add(jsave);
		
		JMenuItem jm=new JMenuItem("另存为(A)");
		jm.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
		menuFile.add(jm);
		menuFile.addSeparator();
		//JMenuItem jm1=new JMenuItem("打印");
		//menuFile.addSeparator();
		//jm1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
		//menuFile.add(jm1);
		JMenuItem jm2=new JMenuItem("插入日期");
		jm2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK));
		JMenuItem jm3=new JMenuItem("全选");
		jm3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
		JMenuItem jm4=new JMenuItem("自动换行");
		jm4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,InputEvent.CTRL_MASK));
		JMenuItem jm5=new JMenuItem("字体设置");
		jm5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));
		
		menuEdit.add(jm2);
		menuEdit.add(jm3);
		menuformat.add(jm4);
		menuformat.add(jm5);
		menuFile.add(jexit);
		menuEdit.addSeparator();
		menuEdit.add(jcut);
		menuEdit.add(jcopy);
		menuEdit.add(jpaste);
		
		menuAbout.add(jabout);
		jm.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				int i = filechooser.showSaveDialog(EditorDemo.this); //显示保存文件对话框
				if (i == JFileChooser.APPROVE_OPTION) {  //点击对话框中保存按钮
					File f = filechooser.getSelectedFile(); //得到选择的文件
					try {
						FileOutputStream out = new FileOutputStream(f);  //得到文件输出流
						out.write(textPane.getText().getBytes()); //写出文件
					} catch (Exception ex) {
						ex.printStackTrace(); //输出出错信息
					}
				}
			}
		});
		/*/jm1.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			  {

			  }

		});*/
		jm2.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			  {
				Date d=new Date();
			   textPane.append(d.toString());
			  }
		});
		jm3.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				textPane.selectAll();
			}
		});
		jm4.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (textPane.getLineWrap()) {
		            textPane.setLineWrap(false);
		        } else {
		            textPane.setLineWrap(true);
		        }
			}
		});
		jm5.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JDialog jd=new JDialog();
				Container con=jd.getContentPane();
				con.setLayout(new FlowLayout());
				jc1=new JComboBox(s1);
				jc2=new JComboBox(s2);
				jc3=new JComboBox(s3);
				jc4=new JComboBox(s4);
				jc5=new JComboBox(s5);
				jc1.addItemListener(new ItemListener()
				{
					public void itemStateChanged(ItemEvent e)
					{
						if(e.getSource()==jc1){
							int n=jc1.getSelectedIndex();
							zihao=Integer.parseInt(s1[n]);
						}
						textPane.setFont(new Font(ziti,style,zihao));
					}
				});
				jc2.addItemListener(new ItemListener()
				{
					public void itemStateChanged(ItemEvent e)
					{
						if(e.getSource()==jc2){
							int m=jc2.getSelectedIndex();
							switch(m)
							{
							case 0:
								style=Font.PLAIN;break;
							case 1:
								style=Font.BOLD;break;
							case 2:
								style=Font.ITALIC;break;
							case 3:
								style=Font.BOLD+Font.ITALIC;break;
							}
						}
						textPane.setFont(new Font(ziti,style,zihao));
					}
				});
				jc3.addItemListener(new ItemListener()
				{
					public void itemStateChanged(ItemEvent e)
					{
						if(e.getSource()==jc3){
							int x=jc3.getSelectedIndex();							
							ziti=s3[x];
						}
						textPane.setFont(new Font(ziti,style,zihao));
					}
				});
				jc4.addItemListener(new ItemListener()
				{
					public void itemStateChanged(ItemEvent e)
					{
						int y=jc4.getSelectedIndex();
						switch(y)
						{
						case 0:
							textPane.setForeground(Color.black);break;
						case 1:
							textPane.setForeground(Color.green);break;
						case 2:
							textPane.setForeground(Color.cyan);break;
						case 3:
							textPane.setForeground(Color.red);break;
						case 4:
							textPane.setForeground(Color.lightGray);break;
						case 5:
							textPane.setForeground(Color.orange);break;
						case 6:
							textPane.setForeground(Color.pink);break;
						case 7:
							textPane.setForeground(Color.yellow);break;
						
						
					}
				}});
				jc5.addItemListener(new ItemListener()
				{
					public void itemStateChanged(ItemEvent e)
					{
						int z=jc5.getSelectedIndex();
						switch(z)
						{
						case 0:
							textPane.setBackground(Color.black);break;
						case 1:
							textPane.setBackground(Color.green);break;
						case 2:
							textPane.setBackground(Color.cyan);break;
						case 3:
							textPane.setBackground(Color.red);break;
						case 4:
							textPane.setBackground(Color.lightGray);break;
						case 5:
							textPane.setBackground(Color.orange);break;
						case 6:
							textPane.setBackground(Color.pink);break;
						case 7:
							textPane.setBackground(Color.yellow);break;
						}
						
					
					}});
				
				con.add(jc1);
				con.add(jc2);
				con.add(jc3);
				con.add(jc4);
				con.add(jc5);
				jd.setSize(300,100);
				jd.setVisible(true);
			}
		});
		menubar.add(menuFile); //增加菜单
		menubar.add(menuEdit);
		menubar.add(menuformat);
		menubar.add(menulook);
		menubar.add(menuAbout);

		return menubar; //返回菜单栏
	}



	private JToolBar createJToolBar(Action[] actions) { //创建工具条
		JToolBar toolBar = new JToolBar(); //实例化工具条
		for (int i = 0; i < actions.length; i++) {
			JButton bt = new JButton(actions[i]); //实例化新的按钮
			bt.setRequestFocusEnabled(false); //设置不需要焦点
			toolBar.add(bt); //增加按钮到工具栏
		}
		return toolBar;  //返回工具栏
	}

	class NewAction extends AbstractAction { //新建文件命令
		public NewAction() {
			super("新建");
		}
		public void actionPerformed(ActionEvent e) {
			textPane.setDocument(new DefaultStyledDocument()); //清空文档
			textPane.setFont(new Font(ziti,style,zihao));
			textPane.setBackground(Color.white);
			textPane.setForeground(Color.black);
		}
	}

	class OpenAction extends AbstractAction { //打开文件命令
		public OpenAction() {
			super("打开");
		}
		public void actionPerformed(ActionEvent e) {
			int i = filechooser.showOpenDialog(EditorDemo.this); //显示打开文件对话框
			if (i == JFileChooser.APPROVE_OPTION) { //点击对话框中打开选项
				File file = filechooser.getSelectedFile(); //得到选择的文件
				try{
                    FileInputStream fin =new FileInputStream(file);
                    InputStreamReader in =new InputStreamReader(fin);
                    BufferedReader reader =new BufferedReader(in);
                    String s=reader.readLine();
                    textPane.setText(s);
                    while((s=reader.readLine())!=null){
                        textPane.append("\n"+s);
                    }
                    
                    in.close();
				}catch(FileNotFoundException fe){}
				catch (Exception ex) {
					ex.printStackTrace();  //输出出错信息
				}
			}
		}
	}

	class SaveAction extends AbstractAction {  //保存命令
		public SaveAction() {
			super("保存");
		}
		public void actionPerformed(ActionEvent e) {
			int i = filechooser.showSaveDialog(EditorDemo.this); //显示保存文件对话框
			if (i == JFileChooser.APPROVE_OPTION) {  //点击对话框中保存按钮
				File f = filechooser.getSelectedFile(); //得到选择的文件
				try {
					FileOutputStream out = new FileOutputStream(f);  //得到文件输出流
					out.write(textPane.getText().getBytes()); //写出文件
				} catch (Exception ex) {
					ex.printStackTrace(); //输出出错信息
				}
			}
		}
	}

	class ExitAction extends AbstractAction { //退出命令
		public ExitAction() {
			super("退出");
		}
		public void actionPerformed(ActionEvent e) {
			System.exit(0);  //退出程序
		}
	}

	class CutAction extends AbstractAction {  //剪切命令
		public CutAction() {
			super("剪切");
		}
		public void actionPerformed(ActionEvent e) {
			textPane.cut();  //调用文本窗格的剪切命令
		}
	}

	class CopyAction extends AbstractAction {  //拷贝命令
		public CopyAction() {
			super("拷贝");
		}
		public void actionPerformed(ActionEvent e) {
			textPane.copy();  //调用文本窗格的拷贝命令
		}
	}

	class PasteAction extends AbstractAction {  //粘贴命令
		public PasteAction() {
			super("粘贴");
		}
		public void actionPerformed(ActionEvent e) {
			textPane.paste();  //调用文本窗格的粘贴命令
		}
	}

	class AboutAction extends AbstractAction { //关于选项命令
		public AboutAction() {
			super("关于");
		}
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(EditorDemo.this, "简单的文本编辑器演示"); //显示软件信息
		}
	}

	public static void main(String[] args) {
		new EditorDemo();
	}

}

⌨️ 快捷键说明

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