Notepad Find

 package notepad;

import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.text.BadLocationException;

public class Find implements ActionListener
{

 private static final long serialVersionUID = 1L;

 private JLabel jLabelFind, jLabelReplace;

 private JTextField jTextFind, jTextReplace;

 private JButton jBtnFind, jBtnReplace, jBtnReplaceAll, jBtnCancel;

 private JCheckBox jUpperOrLower;

 private JRadioButton jRadioUp, jRadioDn;

 private JTextArea jNotepad;

 private JDialog findDialog;

 private int startIndex = -1;

 public Find(JNotepad owner, JTextArea jText)
 {
  jNotepad = jText;
  jLabelFind = new JLabel("查找:");
  jLabelFind.setBounds(16, 22, 42, 22);
  jLabelReplace = new JLabel("替换:");
  jLabelReplace.setBounds(16, 52, 42, 22);
  jTextFind = new JTextField();
  jTextFind.setBounds(60, 22, 180, 22);
  jTextReplace = new JTextField();
  jTextReplace.setBounds(60, 52, 180, 22);
  jBtnFind = new JButton("查找(F)");
  jBtnFind.setMnemonic('F');
  jBtnFind.setBounds(250, 22, 102, 22);
  jBtnFind.addActionListener(this);
  jBtnReplace = new JButton("替换(R)");
  jBtnReplace.setMnemonic('R');
  jBtnReplace.setBounds(250, 52, 102, 22);
  jBtnReplace.addActionListener(this);
  jBtnReplaceAll = new JButton("全部替换(A)");
  jBtnReplaceAll.setMnemonic('A');
  jBtnReplaceAll.setBounds(250, 82, 102, 22);
  jBtnReplaceAll.addActionListener(this);
  jBtnCancel = new JButton("取消(X)");
  jBtnCancel.setMnemonic('X');
  jBtnCancel.setBounds(250, 112, 102, 22);
  jBtnCancel.addActionListener(this);
  jUpperOrLower = new JCheckBox("区分大小写(C)", false);
  jUpperOrLower.setMnemonic('C');
  jUpperOrLower.setBounds(14, 100, 106, 22);
  jRadioUp = new JRadioButton("向上", false);
  jRadioUp.setBounds(132, 100, 52, 22);
  jRadioDn = new JRadioButton("向下", true);
  jRadioDn.setBounds(184, 100, 52, 22);
  ButtonGroup group = new ButtonGroup();
  group.add(jRadioUp);
  group.add(jRadioDn);
  findDialog = new JDialog(owner, "查找/替换");
  Container findPane = findDialog.getContentPane();
  findPane.setLayout(null);
  findPane.add(jLabelFind, null);
  findPane.add(jLabelReplace, null);
  findPane.add(jTextFind, null);
  findPane.add(jTextReplace, null);
  findPane.add(jBtnFind, null);
  findPane.add(jBtnReplace, null);
  findPane.add(jBtnReplaceAll, null);
  findPane.add(jBtnCancel, null);
  findPane.add(jUpperOrLower, null);
  findPane.add(jRadioUp, null);
  findPane.add(jRadioDn, null);
  findDialog.setBounds(400, 200,  372, 188);
  findDialog.setModal(false); // 无模式
  findDialog.setResizable(false); // 固定窗体大小
  findDialog.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    jTextReplace.setText(null);
    jTextFind.setText(null);
    jTextFind.requestFocusInWindow();
   }
  });
 }

 public void setVisible(boolean flag)
 {
  jTextFind.requestFocusInWindow();
  findDialog.setVisible(flag);
 }

 public void actionPerformed(ActionEvent e)
 {
  if(e.getSource().equals(jBtnFind))
  {
   if(jRadioDn.isSelected())
   {
    if(jUpperOrLower.isSelected())
     startIndex = jNotepad.getText().indexOf(jTextFind.getText(), jNotepad.getSelectionEnd());
    else
     startIndex = jNotepad.getText().toLowerCase().indexOf(jTextFind.getText().toLowerCase(), jNotepad.getSelectionEnd());
   }
   else
   {
    try
    {
     if(jUpperOrLower.isSelected())
      startIndex = jNotepad.getText(0, jNotepad.getSelectionStart()).lastIndexOf(jTextFind.getText());
     else
      startIndex = jNotepad.getText(0, jNotepad.getSelectionStart()).toLowerCase().lastIndexOf(jTextFind.getText().toLowerCase());
    }
    catch(BadLocationException se)
    {
     System.out.print(se.getMessage());
    }
   }
   if(startIndex > -1)
    jNotepad.select(startIndex, startIndex + jTextFind.getText().length());
   else
   {
    JOptionPane.showMessageDialog(findDialog, String.format("找不到“%s” 已完成对文档的搜索。", jTextFind.getText()), "记事本", 1);
    jTextReplace.setText(null);
    jTextFind.requestFocusInWindow();
    jTextFind.selectAll();
   }
  }
  else if(e.getSource().equals(jBtnReplace))
  {
   jNotepad.replaceSelection(jTextReplace.getText());
   jNotepad.select(jNotepad.getSelectionStart() - jTextReplace.getText().length(), jNotepad.getSelectionStart());
  }
  else if(e.getSource().equals(jBtnReplaceAll))
  {
   jNotepad.setSelectionStart(0);
   while(jNotepad.getSelectionStart() < jNotepad.getText().length())
   {
    if(jUpperOrLower.isSelected())
     startIndex = jNotepad.getText().indexOf(jTextFind.getText(), jNotepad.getSelectionStart());
    else
     startIndex = jNotepad.getText().toLowerCase().indexOf(jTextFind.getText().toLowerCase(), jNotepad.getSelectionStart());
    if(startIndex > -1)
    {
     jNotepad.select(startIndex, startIndex + jTextFind.getText().length());
     jNotepad.replaceSelection(jTextReplace.getText());
    }
    else
     break;
   }
  }
  else
  {
   jTextReplace.setText(null);
   jTextFind.setText(null);
   jTextFind.requestFocusInWindow();
   findDialog.setVisible(false);
  }
 }

}

### 关于 Notepad 的使用教程、功能介绍及操作指南 #### 功能概述 Windows 自带的 Notepad 是一款轻量级文本编辑工具,主要用于纯文本文件的创建和编辑。其界面简洁,适合快速记录笔记或编写简单的代码片段[^1]。 #### 文件处理 - **新建文件**:启动 Notepad 后,默认会打开一个新的空白文档。 - **保存文件**:点击菜单栏上的 `File` -> `Save As...` 可以指定路径并命名来保存当前正在编辑的内容;对于已有的文件,则可以直接选择 `Save` 进行覆盖保存。 - **打开现有文件**:通过 `File` -> `Open...` 浏览本地磁盘找到目标文件后双击即可加载到编辑区显示出来供修改查看。 #### 编辑命令 - **撤销与重做**:利用快捷键 Ctrl+Z 实现最近一次更改动作的撤回,而按下 Ctrl+Y 则能恢复被取消的操作。 - **剪切复制粘贴**:高亮选中部分文字之后分别对应着 Ctrl+X (Cut), Ctrl+C (Copy) 和 Ctrl+V (Paste),方便用户在不同位置间转移所需内容。 - **查找替换字符串**:借助 `Edit` 菜单下的 `Find...`, 或者直接输入组合键 Ctrl+F 打开对话框,在其中填写待定位的关键字完成精准匹配查询工作;同样地,“Replace…”选项允许批量替换单词短语等元素。 #### 格式设置 尽管原生版本并不支持复杂的样式定制,但仍提供了一些基本的文字排布调整手段: - **字体设定**:经由 `Format` 下拉列表里的 “Font…” 项进入配置窗口自定义正文字体名称、字号以及颜色属性。 - **自动换行控制**:勾选 / 取消 `Word Wrap` 即可切换是否启用软折行模式,使得过长的一行能够适应窗口宽度自然延续至下一行展示而不必手动插入硬回车符。 考虑到 Notepad 较为有限的功能集,当面对更复杂的需求时建议转向其他更为专业的替代品如 Notepad++, Sublime Text, Visual Studio Code 等,它们不仅具备上述提到的基础能力而且还额外提供了诸如语法高亮、宏录制播放等一系列增强特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值