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

📄 notepad.java

📁 用java编写的一个记事本小程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//package P1;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import javax.swing.JColorChooser;
class Notepad extends Frame implements ActionListener, TextListener,WindowListener,KeyListener
{
	MenuBar theMenuBar;
	Menu file,edit,format,help;
	MenuItem newFile, open, save, saveas,exit;
	MenuItem undo, copy, cut, paste,search,searchAndChange;
	MenuItem font;
	MenuItem about;
	TextArea content;
	FileDialog filesource;
	Frame replace;
	//Dialog find;
	//Dialog 
	Label message;
	int firstUndo = 1;
	int firstBackspace = 1;
	String tmp="", t;
	String source=null;
	//String copytmp="";
	Notepad()
	{
		setTitle("Notepad");
		theMenuBar = new MenuBar();

		file = new Menu("File");
		edit = new Menu("Edit");
		format = new Menu("Format");
		help = new Menu("Help");
		
		newFile = new MenuItem("New");
		open = new MenuItem("Open...");
		save = new MenuItem("Save...");
		saveas = new MenuItem("Save As...");
		exit = new MenuItem("Exit");
		undo = new MenuItem("Undo");
		copy = new MenuItem("Copy");
		cut = new MenuItem("Cut");
		paste = new MenuItem("Paste");
		search = new MenuItem("Find...");
		searchAndChange = new MenuItem("Replace...");
		font = new MenuItem("Font...");
		about = new MenuItem("About...");

		//search = new Frame("Search and Change");
		content = new TextArea();
		content.addTextListener(this);
		//fileopen = new FileDialog();

		newFile.addActionListener(this);
		open.addActionListener(this);
		save.addActionListener(this);
		saveas.addActionListener(this);
		exit.addActionListener(this);
		undo.addActionListener(this);
		copy.addActionListener(this);
		cut.addActionListener(this);
		paste.addActionListener(this);
		search.addActionListener(this);
		searchAndChange.addActionListener(this);
		font.addActionListener(this);
		about.addActionListener(this);

		addWindowListener(this);
		content.addKeyListener(this);
		message = new Label();

		theMenuBar.add(file); theMenuBar.add(edit); theMenuBar.add(format); theMenuBar.add(help);

		file.add(newFile); file.addSeparator(); file.add(open); file.add(save); file.add(saveas);	file.addSeparator(); file.add(exit);
		edit.add(undo); edit.addSeparator(); edit.add(copy); edit.add(cut); edit.add(paste); edit.addSeparator(); edit.add(search); edit.add(searchAndChange);
		format.add(font);
		help.add(about);
		setMenuBar(theMenuBar);
		add(content); add(message, "South");
		setSize(400, 400);
		this.setLocation(450, 200);
		setVisible(true);
	}

	public void newfile()
	{
		
		content.setText("");
		setTitle("Notepad");
		firstUndo = 1;
		firstBackspace = 1;
		message.setText("New");
	}
	public class Ask extends Dialog implements ActionListener, WindowListener
	{
		Label lb;
		Button bt1;
		Button bt2;
		Button bt3;
		Panel pn1;
		
		Ask(Frame frame,String s,boolean b)
		{
			super(frame, s, b);
			
			//find = new Dialog(frame, s, b);
			lb = new Label("Do you want to save?");
			bt1 = new Button("Yes");
			bt2 = new Button("No");
			bt3 = new Button("Cancel");	
			pn1 = new Panel();
			setLayout(new GridLayout(2, 1));	
			pn1.setLayout(new GridLayout(1,3));
			pn1.add(bt1);
			pn1.add(bt2);
			pn1.add(bt3);
			add(lb);
			add(pn1);
			setSize(300, 150);
			this.setLocation(500, 300);
			setVisible(true);
			addWindowListener(this);
			bt1.addActionListener(this);
			bt2.addActionListener(this);
			bt3.addActionListener(this);
		}
		public void windowClosing(WindowEvent e)
		{ dispose(); }
		public void windowOpened(WindowEvent e) { }
		public void windowActivated(WindowEvent e) { }
		public void windowDeactivated(WindowEvent e) { }
		public void windowClosed(WindowEvent e) { }
		public void windowIconified(WindowEvent e) { }
		public void windowDeiconified(WindowEvent e) { }

		public void actionPerformed(ActionEvent ae)
		{
			if (ae.getSource().equals(bt1))
			{
				saveas();
				System.exit(0);
			}
			if (ae.getSource().equals(bt2))
			{
				newfile();
				dispose();
			}
			if (ae.getSource().equals(bt3))
			{
				dispose();
			}
		}
	}
	
	public class Ask2 extends Dialog implements ActionListener, WindowListener
	{
		Label lb;
		Button bt1;
		Button bt2;
		Button bt3;
		Panel pn1;
		
		Ask2(Frame frame,String s,boolean b)
		{
			super(frame, s, b);
			
			//find = new Dialog(frame, s, b);
			lb = new Label("Do you want to save?");
			bt1 = new Button("Yes");
			bt2 = new Button("No");
			bt3 = new Button("Cancel");	
			pn1 = new Panel();
			setLayout(new GridLayout(2, 1));	
			pn1.setLayout(new GridLayout(1,3));
			pn1.add(bt1);
			pn1.add(bt2);
			pn1.add(bt3);
			add(lb);
			add(pn1);
			setSize(300, 150);
			this.setLocation(500, 300);
			setVisible(true);
			addWindowListener(this);
			bt1.addActionListener(this);
			bt2.addActionListener(this);
			bt3.addActionListener(this);
		}
		public void windowClosing(WindowEvent e)
		{ dispose(); }
		public void windowOpened(WindowEvent e) { }
		public void windowActivated(WindowEvent e) { }
		public void windowDeactivated(WindowEvent e) { }
		public void windowClosed(WindowEvent e) { }
		public void windowIconified(WindowEvent e) { }
		public void windowDeiconified(WindowEvent e) { }

		public void actionPerformed(ActionEvent ae)
		{
			if (ae.getSource().equals(bt1))
			{
				saveas();
				System.exit(0);
			}
			if (ae.getSource().equals(bt2))
			{
				System.exit(0);
			}
			if (ae.getSource().equals(bt3))
			{
				dispose();
			}
		}
	}
	public void open()
	{
		try
		{

			filesource = new FileDialog(this, "open", FileDialog.LOAD);
			filesource.show();
			source = filesource.getDirectory() + filesource.getFile();
			DataInputStream fileRead = new DataInputStream(new FileInputStream(source));
			String line="";
			content.setText("");
			while ((line = fileRead.readLine()) != null)
				content.append(line + "\n");
			setTitle(filesource.getFile());
			fileRead.close();
			message.setText(source);
			
			

		}
		catch (Exception e) { message.setText(e.getMessage()); }
	
	}

	public void save()
	{
		try
		{
			if (source==null)
			{
			filesource = new FileDialog(this, "save", FileDialog.SAVE);
			filesource.show();
			source = filesource.getDirectory() + filesource.getFile();
			DataOutputStream fileWrite = new DataOutputStream(new FileOutputStream(source));
			String info = content.getText();
			if (info == null || info.length() < 1)
				message.setText("cannot save the empty information");
			else
				fileWrite.writeBytes(info);
			setTitle(filesource.getFile());
			fileWrite.close();
			message.setText(source);
			}
			else
			{
				DataOutputStream fileWrite = new DataOutputStream(new FileOutputStream(source));
				String info = content.getText();
				if (info == null || info.length() < 1)
					message.setText("cannot save the empty information");
				else
					fileWrite.writeBytes(info);
				setTitle(filesource.getFile());
				fileWrite.close();
				message.setText(source);
			}
		}
		catch (Exception e) { message.setText(e.getMessage()); }
	}
	public void saveas()
	{
		try
		{
			
			filesource = new FileDialog(this, "save", FileDialog.SAVE);
			this.setLocation(500, 300);
			filesource.show();
			source = filesource.getDirectory() + filesource.getFile();
			DataOutputStream fileWrite = new DataOutputStream(new FileOutputStream(source));
			String info = content.getText();
			if (info == null || info.length() < 1)
				message.setText("cannot save the empty information");
			else
				fileWrite.writeBytes(info);
			setTitle(filesource.getFile());
			fileWrite.close();
			message.setText(source);
			
		}
		catch (Exception e) { message.setText(e.getMessage()); }
	}

	public void exit()
	{
		System.exit(0);
	}
//above is the file menu
	public void undo()
	{
		
		/*if (firstUndo == 1&&(firstBackspace!=2))
		{
			tmp = "";
			firstUndo++;
		
		}*/
		t = content.getText();
		content.setText(tmp);
		tmp = t;
		firstBackspace = 1;
		message.setText("File content Modified");
	}

	public void copy()
	{
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		String copytmp = content.getSelectedText();
		StringSelection stringSelection = new StringSelection(copytmp);
		clipboard.setContents(stringSelection, null);


	}

	public void cut()
	{
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		String temp = content.getSelectedText();
		StringSelection stringSelection = new StringSelection(temp);
		clipboard.setContents(stringSelection, null);
		int start = content.getSelectionStart();
		int end = content.getSelectionEnd();
		content.replaceRange("", start, end);
		message.setText("File content Modified");
	}

	public void paste()
	{
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		Transferable transferable = clipboard.getContents(this);
		DataFlavor flavor = DataFlavor.stringFlavor;
		if (transferable.isDataFlavorSupported(flavor))
		{
			try
			{
				int start = content.getSelectionStart();
				int end = content.getSelectionEnd();
				content.replaceRange("", start, end);
				String str;
				int n = content.getCaretPosition();
				str = (String)transferable.getTransferData(flavor);
				content.insert(str, n);
				message.setText("File content Modified");
			}
			catch (Exception ee)
			{

			}
		}
		
	}
	public class Find extends Dialog implements ActionListener, WindowListener
	{
		Label lb;
		TextField tf;
		Button bt;
		int start = 1;
		String allcontent =content.getText();
		String target=null;
		int length;
		Find(Frame frame,String s,boolean b)
		{
			super(frame, s, b);
			//find = new Dialog(frame, s, b);
			lb = new Label("");
			tf = new TextField(10);
			bt = new Button("Find");
			setLayout(new GridLayout(4, 2));
			add(new Label(""));
			add(new Label(""));
			add(tf);
			add(bt);
			add(new Label(""));
			add(lb);
			setSize(400, 200);
			this.setLocation(500, 300);
			setVisible(true);
			addWindowListener(this);
			bt.addActionListener(this);
		}
		public void windowClosing(WindowEvent e)
		{ dispose(); }
		public void windowOpened(WindowEvent e) { }
		public void windowActivated(WindowEvent e) { }
		public void windowDeactivated(WindowEvent e) { }

⌨️ 快捷键说明

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