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

📄 notepad.java

📁 用java写的记事本程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

class Searcher extends JDialog
{
	private JLabel Sample = new JLabel();
	private int curRow, curCol;
	private JTextField tfS;
	private JCheckBox chkCaseSensible;
	private JButton btnOK;
	private JButton btnCancel;
	private Searcher dlg;
	public static SchInfo schInfo;
	private JTextArea ta;
	public Searcher(Frame parent, boolean modal, JTextArea _ta)
	{
		super(parent,modal);
		schInfo=new SchInfo();
		ta=_ta;
		Container ctn=getContentPane();
		JPanel ctn1=new JPanel();
		JPanel ctn2=new JPanel();
		JPanel ctn3=new JPanel();
		JPanel ctn4=new JPanel();
		btnOK=new JButton("查找下一个");
		btnCancel=new JButton("取消");
		initAll();
		chkCaseSensible=new JCheckBox("区分大小写", false);
		tfS=new JTextField(10);
		ctn.setLayout(new FlowLayout());
		ctn.add(ctn1);
		ctn.add(ctn2);
		ctn1.setLayout(new GridLayout(2, 1));
		ctn1.add(ctn3);
		ctn1.add(ctn4);
		ctn3.setLayout(new FlowLayout());
		ctn4.setLayout(new FlowLayout());
		ctn3.add(new JLabel("查找内容:"));
		ctn3.add(tfS);
		ctn4.add(chkCaseSensible);
		ctn2.setLayout(new GridLayout(2, 1));
		ctn2.add(btnOK);
		ctn2.add(btnCancel);
		setTitle("查找");
		btnOK.addActionListener(
		new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				schInfo.str=tfS.getText();
				schInfo.caseSensible=chkCaseSensible.isSelected();
				String str=ta.getText();
				int index;
				if (schInfo.caseSensible)
					index=str.indexOf(schInfo.str, ta.getCaretPosition());
				else
					index=str.toUpperCase().indexOf(schInfo.str.toUpperCase(), ta.getCaretPosition());
				if (index!=-1)
				{
					ta.select(index, index+schInfo.str.length());
				}
				else
					JOptionPane.showMessageDialog(null,
						"找不到\""+schInfo.str+"\"",
						"记事本",
						JOptionPane.INFORMATION_MESSAGE);
			}
		});
		btnCancel.addActionListener(
		new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				schInfo.str="";
				schInfo.caseSensible=false;
				setVisible(false);
			}
		});
		setVisible(true);
	}
	private void initAll()
	{
		setSize(400,140);
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(java.awt.event.WindowEvent e)
			{
				setVisible (false);
			}
		});
	}
}
class SchInfo
{
	public String str;
	public boolean caseSensible;
}

class Replacer extends JDialog
{
	private JLabel Sample = new JLabel();
	private int curRow, curCol;
	private JTextField tfS, tfR;
	private JCheckBox chkCaseSensible;
	private JButton btnOK, btnReplace;
	private JButton btnCancel;
	private Replacer dlg;
	public static RplInfo schInfo;
	private JTextArea ta;
	public Replacer(Frame parent, boolean modal, JTextArea _ta)
	{
		super(parent,modal);
		schInfo=new RplInfo();
		ta=_ta;
		Container ctn=getContentPane();
		JPanel ctn1=new JPanel();
		JPanel ctn2=new JPanel();
		JPanel ctn3=new JPanel();
		JPanel ctn4=new JPanel();
		JPanel ctn5=new JPanel();
		btnOK=new JButton("查找下一个");
		btnReplace=new JButton("替换");
		btnCancel=new JButton("取消");
		initAll();
		chkCaseSensible=new JCheckBox("区分大小写", false);
		tfS=new JTextField(10);
		tfR=new JTextField(10);
		ctn.setLayout(new FlowLayout());
		ctn.add(ctn1);
		ctn.add(ctn2);
		ctn1.setLayout(new GridLayout(3,1));
		ctn1.add(ctn3);
		ctn1.add(ctn4);
		ctn1.add(ctn5);
		ctn3.setLayout(new FlowLayout());
		ctn4.setLayout(new FlowLayout());
		ctn5.setLayout(new FlowLayout());
		ctn3.add(new JLabel("查找内容:"));
		ctn3.add(tfS);
		ctn4.add(new JLabel("替换为:"));
		ctn4.add(tfR);
		ctn5.add(chkCaseSensible);
		ctn2.setLayout(new GridLayout(3, 1));
		ctn2.add(btnOK);
		ctn2.add(btnReplace);
		ctn2.add(btnCancel);
		setTitle("替换");
		btnOK.addActionListener(
		new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				schInfo.str=tfS.getText();
				schInfo.caseSensible=chkCaseSensible.isSelected();
				String str=ta.getText();
				int index;
				if (schInfo.caseSensible)
					index=str.indexOf(schInfo.str, ta.getCaretPosition());
				else
					index=str.toUpperCase().indexOf(schInfo.str.toUpperCase(), ta.getCaretPosition());
				if (index!=-1)
				{
					ta.select(index, index+schInfo.str.length());
				}
				else
					JOptionPane.showMessageDialog(null,
						"找不到\""+schInfo.str+"\"",
						"记事本",
						JOptionPane.INFORMATION_MESSAGE);
			}
		});
		btnReplace.addActionListener(
		new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				String str=ta.getSelectedText();
				int index;
				schInfo.str=tfS.getText();
				if (str!=null && str.equals(tfS.getText()))
					index=ta.getCaretPosition()-tfS.getText().length();
				else
				{
					schInfo.caseSensible=chkCaseSensible.isSelected();
					str=ta.getText();
					if (schInfo.caseSensible)
						index=str.indexOf(schInfo.str, ta.getCaretPosition());
					else
						index=str.toUpperCase().indexOf(schInfo.str.toUpperCase(), ta.getCaretPosition());
				}
				str=ta.getText();
				if (index!=-1)
				{
					str.replaceFirst(tfS.getText(), tfR.getText());
					String strTemp=str.substring(0, index)+tfR.getText()+str.substring((index+tfS.getText().length()), str.length());
					str=strTemp;
					ta.setText(str);
					ta.setCaretPosition(index+tfR.getText().length());
				}
				else
					JOptionPane.showMessageDialog(null,
						"找不到\""+schInfo.str+"\"",
						"记事本",
						JOptionPane.INFORMATION_MESSAGE);
			}
		});
		btnCancel.addActionListener(
		new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				schInfo.str="";
				schInfo.caseSensible=false;
				setVisible(false);
			}
		});
		setVisible(true);
	}
	private void initAll()
	{
		setSize(400,140);
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(java.awt.event.WindowEvent e)
			{
				setVisible (false);
			}
		});
	}
}
class RplInfo extends SchInfo
{
	public String strR;
}

public class Notepad extends JFrame implements ActionListener
{
	JTextArea ta;
	JScrollPane scr;
	JMenuBar menuBar;
	JMenu menu[];
	JMenuItem menuItem[][], popupMenuItem[];
	JPopupMenu popupMenu;
	JLabel lblStatus;
	JComboBox cbFont, cbSize, cbStyle;
	boolean changed;
	String curFileName;
	String schText[]=new String[2];
	String[] strFonts;
	String[] strSize={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
	String[] strStyle={"常规","粗体","斜体","粗斜体"};
	Container ctn;
	SchInfo schInfo;
	GraphicsEnvironment ge;
	Font defaultFont=new Font("宋体", Font.PLAIN, 12), fonts[];
	public Notepad()
	{
		super("无标题 - 记事本");
		changed=false;
		curFileName="";
		lblStatus=new JLabel("就绪");
		ta=new JTextArea();
		scr=new JScrollPane(ta);
		ctn=getContentPane();
		ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
		strFonts=ge.getAvailableFontFamilyNames();
		cbFont=new JComboBox(strFonts);
		cbFont.setFont(defaultFont);
		cbFont.setSelectedItem("宋体");
		cbSize=new JComboBox(strSize);
		cbSize.setFont(defaultFont);
		cbSize.setSelectedItem("12");
		cbStyle=new JComboBox(strStyle);
		cbFont.setSelectedItem("常规");
		cbStyle.setFont(defaultFont);
		cbFont.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent ie)
			{
				int size, style;
				String fontName;
				fontName=(String)cbFont.getSelectedItem();
				style=cbStyle.getSelectedIndex();
				size=Integer.parseInt((String)cbSize.getSelectedItem());
				Font font=new Font(fontName, style, size);
				ta.setFont(font);
			}
		});
		cbSize.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent ie)
			{
				int size, style;
				String fontName;
				fontName=(String)cbFont.getSelectedItem();
				style=cbStyle.getSelectedIndex();
				size=Integer.parseInt((String)cbSize.getSelectedItem());
				Font font=new Font(fontName, style, size);
				ta.setFont(font);
			}
		});
		cbStyle.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent ie)
			{
				int size, style;
				String fontName;
				fontName=(String)cbFont.getSelectedItem();
				style=cbStyle.getSelectedIndex();
				size=Integer.parseInt((String)cbSize.getSelectedItem());
				Font font=new Font(fontName, style, size);
				ta.setFont(font);
			}
		});
		cbFont.setToolTipText("在这里设置字体");
		cbSize.setToolTipText("在这里设置字体大小");
		cbStyle.setToolTipText("在这里设置字体风格");
		JPanel panel=new JPanel();
		panel.add(cbFont);
		panel.add(cbSize);
		panel.add(cbStyle);
		ctn.setLayout(new BorderLayout());
		ctn.add(panel, BorderLayout.NORTH);
		ctn.add(scr, BorderLayout.CENTER);
		ctn.add(lblStatus, BorderLayout.SOUTH);
		showMenu();
		schText[0]=new String("");
		schText[1]=new String("");
		lblStatus.setFont(defaultFont);
		ta.setFont(defaultFont);
		ta.getDocument().addDocumentListener(new DocumentListener()
		{
			public void changedUpdate(DocumentEvent de)
			{
				lblStatus.setText("行数:"+ta.getLineCount());
				changed=true;
			}
			public void insertUpdate(DocumentEvent de)
			{
				lblStatus.setText("行数:"+ta.getLineCount());
				changed=true;
			}
			public void removeUpdate(DocumentEvent de)
			{
				lblStatus.setText("行数:"+ta.getLineCount());
				changed=true;
			}
		}
		);
		addWindowListener(
		new WindowAdapter()
		{
			public void windowClosing(WindowEvent we)
			{

⌨️ 快捷键说明

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