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

📄 notepad备份.java

📁 这是本人在大二下暑假期间用JAVA写的记事本
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		{	editMenu_Cut.setEnabled(true);
			popupMenu_Cut.setEnabled(true); 
			editMenu_Copy.setEnabled(true);
			popupMenu_Copy.setEnabled(true);
			editMenu_Delete.setEnabled(true);
			popupMenu_Delete.setEnabled(true);
		}
		//粘帖功能可用性判断
		Transferable contents=clipBoard.getContents(this);
		if(contents==null)
		{	editMenu_Paste.setEnabled(false);
			popupMenu_Paste.setEnabled(false);
		}
		else
		{	editMenu_Paste.setEnabled(true);
			popupMenu_Paste.setEnabled(true);	
		}
	}//方法checkMenuItemEnabled()结束

	//关闭窗口时调用
	public void exitWindowChoose()
	{	editArea.requestFocus();
		String currentValue=editArea.getText();
		if(currentValue.equals(oldValue)==true)
		{	System.exit(0);
		}
		else
		{	int exitChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存,是否保存?","退出提示",JOptionPane.YES_NO_CANCEL_OPTION);
			if(exitChoose==JOptionPane.YES_OPTION)
			{	if(isNewFile)
				{	
					String str=null;
					JFileChooser fileChooser=new JFileChooser();
					fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
					fileChooser.setApproveButtonText("确定");
					fileChooser.setDialogTitle("另存为");
					
					int result=fileChooser.showSaveDialog(this);
					
					if(result==JFileChooser.CANCEL_OPTION)
					{	statusLabel.setText(" 您没有保存文件");
						return;
					}					
	
					File saveFileName=fileChooser.getSelectedFile();
				
					if(saveFileName==null||saveFileName.getName().equals(""))
					{	JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
					}
					else 
					{	try
						{	FileWriter fw=new FileWriter(saveFileName);
							BufferedWriter bfw=new BufferedWriter(fw);
							bfw.write(editArea.getText(),0,editArea.getText().length());
							bfw.flush();
							fw.close();
							
							isNewFile=false;
							currentFile=saveFileName;
							oldValue=editArea.getText();
							
							this.setTitle(saveFileName.getName()+"  - 记事本");
							statusLabel.setText(" 当前打开文件:"+saveFileName.getAbsoluteFile());
						}							
						catch(IOException ioException){					
						}				
					}
				}
				else
				{
					try
					{	FileWriter fw=new FileWriter(currentFile);
						BufferedWriter bfw=new BufferedWriter(fw);
						bfw.write(editArea.getText(),0,editArea.getText().length());
						bfw.flush();
						fw.close();
					}							
					catch(IOException ioException){					
					}
				}
				System.exit(0);
			}
			else if(exitChoose==JOptionPane.NO_OPTION)
			{	System.exit(0);
			}
			else
			{	return;
			}
		}
	}//关闭窗口时调用方法结束

	//查找方法
	public void find()
	{	final JDialog findDialog=new JDialog(this,"查找",false);//false时允许其他窗口同时处于激活状态(即无模式)
		Container con=findDialog.getContentPane();//返回此对话框的contentPane对象	
		con.setLayout(new FlowLayout(FlowLayout.LEFT));
		JLabel findContentLabel=new JLabel("查找内容(N):");
		final JTextField findText=new JTextField(15);
		JButton findNextButton=new JButton("查找下一个(F):");
		final JCheckBox matchCheckBox=new JCheckBox("区分大小写(C)");
		ButtonGroup bGroup=new ButtonGroup();
		final JRadioButton upButton=new JRadioButton("向上(U)");
		final JRadioButton downButton=new JRadioButton("向下(U)");
		downButton.setSelected(true);
		bGroup.add(upButton);
		bGroup.add(downButton);
		/*ButtonGroup此类用于为一组按钮创建一个多斥(multiple-exclusion)作用域。
		使用相同的 ButtonGroup 对象创建一组按钮意味着“开启”其中一个按钮时,将关闭组中的其他所有按钮。*/
		/*JRadioButton此类实现一个单选按钮,此按钮项可被选择或取消选择,并可为用户显示其状态。
		与 ButtonGroup 对象配合使用可创建一组按钮,一次只能选择其中的一个按钮。
		(创建一个 ButtonGroup 对象并用其 add 方法将 JRadioButton 对象包含在此组中。)*/
		JButton cancel=new JButton("取消");
		//取消按钮事件处理
		cancel.addActionListener(new ActionListener()
		{	public void actionPerformed(ActionEvent e)
			{	findDialog.dispose();
			}
		});
		//"查找下一个"按钮监听
		findNextButton.addActionListener(new ActionListener()
		{	public void actionPerformed(ActionEvent e)
			{	int k=0,m=0;
				final String str1,str2,str3,str4,strA,strB;
				str1=editArea.getText();
				str2=findText.getText();
				str3=str1.toUpperCase();
				str4=str2.toUpperCase();
				if(matchCheckBox.isSelected())//区分大小写
				{	strA=str1;
					strB=str2;
				}
				else//不区分大小写,此时把所选内容全部化成大写(或小写),以便于查找 
				{	strA=str3;
					strB=str4;
				}
				if(upButton.isSelected())
				{	if(editArea.getSelectedText()==null)
						k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
					else
						k=strA.lastIndexOf(strB, editArea.getCaretPosition()-findText.getText().length()-1);	
					if(k>-1)
					{	editArea.setCaretPosition(k);
						editArea.select(k,k+strB.length());
					}
					else
					{	JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
					}
				}
				else if(downButton.isSelected())
				{	if(editArea.getSelectedText()==null)
						k=strA.indexOf(strB,editArea.getCaretPosition()+1);
					else
						k=strA.indexOf(strB, editArea.getCaretPosition()-findText.getText().length()+1);	
					if(k>-1)
					{	editArea.setCaretPosition(k);
						editArea.select(k,k+strB.length());
					}
					else
					{	JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
					}
				}
			}
		});//"查找下一个"按钮监听结束
		//创建"查找"对话框的界面
		JPanel panel1=new JPanel();
		JPanel panel2=new JPanel();
		JPanel panel3=new JPanel();
		JPanel directionPanel=new JPanel();
		directionPanel.setBorder(BorderFactory.createTitledBorder("方向"));
		//设置directionPanel组件的边框;
		//BorderFactory.createTitledBorder(String title)创建一个新标题边框,使用默认边框(浮雕化)、默认文本位置(位于顶线上)、默认调整 (leading) 以及由当前外观确定的默认字体和文本颜色,并指定了标题文本。
		directionPanel.add(upButton);
		directionPanel.add(downButton);
		panel1.setLayout(new GridLayout(2,1));
		panel1.add(findNextButton);
		panel1.add(cancel);
		panel2.add(findContentLabel);
		panel2.add(findText);
		panel2.add(panel1);
		panel3.add(matchCheckBox);
		panel3.add(directionPanel);
		con.add(panel2);
		con.add(panel3);
		findDialog.setSize(410,180);
		findDialog.setResizable(false);//不可调整大小
		findDialog.setLocation(230,280);
		findDialog.setVisible(true);
	}//查找方法结束
	
	//替换方法
	public void replace()
	{	final JDialog replaceDialog=new JDialog(this,"替换",false);//false时允许其他窗口同时处于激活状态(即无模式)
		Container con=replaceDialog.getContentPane();//返回此对话框的contentPane对象
		con.setLayout(new FlowLayout(FlowLayout.CENTER));
		JLabel findContentLabel=new JLabel("查找内容(N):");
		final JTextField findText=new JTextField(15);
		JButton findNextButton=new JButton("查找下一个(F):");
		JLabel replaceLabel=new JLabel("替换为(P):");
		final JTextField replaceText=new JTextField(15);
		JButton replaceButton=new JButton("替换(R)");
		JButton replaceAllButton=new JButton("全部替换(A)");
		JButton cancel=new JButton("取消");
		cancel.addActionListener(new ActionListener()
		{	public void actionPerformed(ActionEvent e)
			{	replaceDialog.dispose();
			}
		});
		final JCheckBox matchCheckBox=new JCheckBox("区分大小写(C)");
		ButtonGroup bGroup=new ButtonGroup();
		final JRadioButton upButton=new JRadioButton("向上(U)");
		final JRadioButton downButton=new JRadioButton("向下(U)");
		downButton.setSelected(true);
		bGroup.add(upButton);
		bGroup.add(downButton);
		/*ButtonGroup此类用于为一组按钮创建一个多斥(multiple-exclusion)作用域。
		使用相同的 ButtonGroup 对象创建一组按钮意味着“开启”其中一个按钮时,将关闭组中的其他所有按钮。*/
		/*JRadioButton此类实现一个单选按钮,此按钮项可被选择或取消选择,并可为用户显示其状态。
		与 ButtonGroup 对象配合使用可创建一组按钮,一次只能选择其中的一个按钮。
		(创建一个 ButtonGroup 对象并用其 add 方法将 JRadioButton 对象包含在此组中。)*/
		
		//"查找下一个"按钮监听
		findNextButton.addActionListener(new ActionListener()
		{	public void actionPerformed(ActionEvent e)
			{	//"区分大小写(C)"的JCheckBox是否被选中
				int k=0,m=0;
				final String str1,str2,str3,str4,strA,strB;
				str1=editArea.getText();
				str2=findText.getText();
				str3=str1.toUpperCase();
				str4=str2.toUpperCase();
				if(matchCheckBox.isSelected())//区分大小写
				{	strA=str1;
					strB=str2;
				}
				else//不区分大小写,此时把所选内容全部化成大写(或小写),以便于查找 
				{	strA=str3;
					strB=str4;
				}
				if(upButton.isSelected())
				{	if(editArea.getSelectedText()==null)
						k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
					else
						k=strA.lastIndexOf(strB, editArea.getCaretPosition()-findText.getText().length()-1);	
					if(k>-1)
					{	editArea.setCaretPosition(k);
						editArea.select(k,k+strB.length());
					}
					else
					{	JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
					}
				}
				else if(downButton.isSelected())
				{	if(editArea.getSelectedText()==null)
						k=strA.indexOf(strB,editArea.getCaretPosition()+1);
					else
						k=strA.indexOf(strB, editArea.getCaretPosition()-findText.getText().length()+1);	
					if(k>-1)
					{	editArea.setCaretPosition(k);
						editArea.select(k,k+strB.length());
					}
					else
					{	JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
					}
				}
			}
		});//"查找下一个"按钮监听结束
		
		//"替换"按钮监听
		replaceButton.addActionListener(new ActionListener()
		{	public void actionPerformed(ActionEvent e)
			{	if(replaceText.getText().length()==0 && editArea.getSelectedText()!=null) 
					editArea.replaceSelection(""); 
				if(replaceText.getText().length()>0 && editArea.getSelectedText()!=null) 
					editArea.replaceSelection(replaceText.getText());
			}
		});//"替换"按钮监听结束
		
		//"全部替换"按钮监听
		replaceAllButton.addActionListener(new ActionListener()
		{	public void actionPerformed(ActionEvent e)
			{	editArea.setCaretPosition(0);	//将光标放到编辑区开头	
				int k=0,m=0,replaceCount=0;
				if(findText.getText().length()==0)
				{	JOptionPane.showMessageDialog(replaceDialog,"请填写查找内容!","提示",JOptionPane.WARNING_MESSAGE);
					findText.requestFocus(true);
					return;
				}
				while(k>-1)//当文本中有内容被选中时(k>-1被选中)进行替换,否则不进行while循环
				{	final String str1,str2,str3,str4,strA,strB;
					str1=editArea.getText();
					str2=findText.getText();
					str3=str1.toUpperCase();
					str4=str2.toUpperCase();
					if(matchCheckBox.isSelected())//区分大小写
					{	strA=str1;
						strB=str2;
					}
					else//不区分大小写,此时把所选内容全部化成大写(或小写),以便于查找 
					{	strA=str3;
						strB=str4;
					}
					if(upButton.isSelected())
					{	if(editArea.getSelectedText()==null)
							k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
						else
							k=strA.lastIndexOf(strB, editArea.getCaretPosition()-findText.getText().length()-1);	
						if(k>-1)
						{	editArea.setCaretPosition(k);
							editArea.select(k,k+strB.length());
						}

⌨️ 快捷键说明

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