首页>代码>java swing实现的学生教师选课管理系统代码>/NotePadTest/src/Find/FindAndReplace.java
package Find;
import Main.MainFrame;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
/**
 *@用于记事本中的查找、查找下一个以及替换的功能的窗口
 * @author 小路
 * @date 2013-05-04
 */
public class FindAndReplace extends JDialog
{
    private MainFrame mf;
    private FindAndReplace far = this;
    private JLabel findwhat = new JLabel("查找内容(N):");
    private JLabel replaceto = new JLabel("  替换为(P):");
    private JTextField findtext = new JTextField("");
    private JTextField replacetext = new JTextField("");
    private JCheckBox distinguish = new JCheckBox("区分大小写");
    private JRadioButton up = new JRadioButton("向上(U)");
    private JRadioButton down = new JRadioButton("向下(D)",true);//默认向下找
    private JButton findnext = new JButton("查找下一个");
    private JButton findall = new JButton("查找全部");
    private JButton replace = new JButton("替换");
    private JButton replaceall = new JButton("全部替换"); 
    private JButton cancle = new JButton("取消");
    private int findnextcount = 0;//用于计数单击了多少次查找下一个
    public FindAndReplace(MainFrame mf)//MainFrame mf
    {
        this.mf = mf;
    }
    public void init()
    {
        //标签、文本框、按钮是否可用
        findnext.setEnabled(false);
        findall.setEnabled(false);
        replace.setEnabled(false);
        replaceall.setEnabled(false);
        //监听器
        findtext.addKeyListener(new KeyAdapter()        
        {
            public void keyReleased(KeyEvent ke) 
            {
               if(findtext.getText().length()!=0)
               {
                   findnext.setEnabled(true);
                   findall.setEnabled(true);
               }
               else
               {
                   findnext.setEnabled(false);
                   findall.setEnabled(false);
               }      
            }    
        });
        replacetext.addKeyListener(new KeyAdapter()
        {
            public void keyReleased(KeyEvent e)
            {
                if(replacetext.getText().length()!=0)
               {
                   replace.setEnabled(true);
                   replaceall.setEnabled(true);
               }
               else
               {
                   replace.setEnabled(false);
                   replaceall.setEnabled(false);
               }
            }
        });
        findnext.setActionCommand("查找下一个");
        findall.setActionCommand("查找全部");
        replace.setActionCommand("替换");
        replaceall.setActionCommand("全部替换");
        cancle.setActionCommand("取消");
        findnext.addActionListener(new MyListener());
        findall.addActionListener(new MyListener());
        replace.addActionListener(new MyListener());
        replaceall.addActionListener(new MyListener());
        cancle.addActionListener(new MyListener());
        //添加查找标签和第一个文本框的box
        Box box1 = Box.createHorizontalBox();
        box1.add(Box.createHorizontalStrut(5));
        box1.add(findwhat);
        box1.add(Box.createHorizontalStrut(5));
        box1.add(findtext);
        box1.add(Box.createHorizontalStrut(5));
        //添加替换标签第二个文本框box
        Box box2 = Box.createHorizontalBox();
        box1.add(Box.createHorizontalStrut(5));
        box2.add(replaceto);
        box2.add(Box.createHorizontalStrut(15));
        box2.add(replacetext);
        box2.add(Box.createHorizontalStrut(10));
        //添加两个单选按钮的Box
        up.setActionCommand("向上");
        down.setActionCommand("向下");
        up.addActionListener(new MyListener());
        down.addActionListener(new MyListener());
        ButtonGroup bg = new ButtonGroup();
        bg.add(up);
        bg.add(down);
        Box box3 = Box.createHorizontalBox();
        box3.setBorder(BorderFactory.createTitledBorder("方向"));
        box3.add(Box.createHorizontalStrut(5));
        box3.add(up);
        box3.add(Box.createHorizontalStrut(5));
        box3.add(down);
        box3.add(Box.createHorizontalStrut(5));
        //添加区分复选框和两个单选按钮的box
        Box box4 = Box.createHorizontalBox();
        box4.add(distinguish);
        box4.add(box3);
        //主窗体的Box
        Box mainbox = Box.createVerticalBox();
        mainbox.add(Box.createVerticalStrut(5));
        mainbox.add(box1);
        mainbox.add(Box.createVerticalStrut(15));
        mainbox.add(box2);
        mainbox.add(Box.createVerticalStrut(15));
        mainbox.add(box4);
        //右边区域的box
        Box box5 = Box.createHorizontalBox();
        box5.add(Box.createHorizontalStrut(5));
        box5.add(findnext);
        box5.add(Box.createHorizontalStrut(3));
        box5.add(findall);
        box5.add(Box.createHorizontalStrut(3));
        box5.add(replace);
        box5.add(Box.createHorizontalStrut(3));
        box5.add(replaceall);
        box5.add(Box.createHorizontalStrut(3));
        box5.add(cancle);
        box5.add(Box.createHorizontalStrut(5));
        mainbox.add(Box.createVerticalStrut(15));
        mainbox.add(box5);
        mainbox.add(Box.createVerticalStrut(5));
        this.add(mainbox);
        this.pack();
        this.setResizable(false);
    }    
private class MyListener implements ActionListener
{
    public void actionPerformed(ActionEvent ae) 
    {
        
        switch(ae.getActionCommand())
        {
            case "向上":
                break;
            case "向下":
                break;
            case "查找下一个":
                findnextcount++;
                findNext();
                break;
            case "查找全部":
                findAll();
                break;
            case "替换":
                replace();
                break;
            case "替换全部":
                replaceAll();
                break;
            case "取消":
                far.removeAll();
                far.dispose();
                break;
        }
    }  
}
   private int findNext()
   {
       //实现向上和向下方向搜素的比较简单,但是实现以光标当前的位置来上下搜素没有达到令人满意的效果,所以暂时没完善
       String find = findtext.getText();
       String main = mf.getMainText().getText().replaceAll("\n", "");
       int count = 1;
       if(!distinguish.isSelected())//不区分大小写
       {
           find = find.toLowerCase();
           main = main.toLowerCase();                  
       }
       //因为在单击按钮的时候光标就已经不在文本域中了,读取光标的位置不太靠谱
       int length = main.length() - find.length() + 1;
       if(length>0)
       {
           int [] start = new int[length];//标记匹配处开头位置,最多有Length个匹配的开头坐标值
            for(int i =0;i<length;i++)
            {
                if(main.substring(i, i+find.length()).equals(find))
                {
                     //要记下i的位置
                     start[count] = i;
                     count++;
                }
            }
            if(findnextcount<count)
            {
                mf.getMainText().setSelectedTextColor(Color.RED);
                if(up.isSelected())
                {
                    mf.getMainText().select(start[count-findnextcount], start[count-findnextcount]+find.length());
                }
                if(down.isSelected())
                {
                    mf.getMainText().select(start[findnextcount], start[findnextcount]+find.length());
                }  
            }
            else
            {
                JOptionPane.showMessageDialog(this, "找不到"+find);
                mf.getMainText().select(0, 0);
                findnextcount = 0;
                count = 1;
                return 0;
            }
       }
       else
       {
           JOptionPane.showMessageDialog(this, "找不到"+find);
           mf.getMainText().select(0, 0);
           findnextcount = 0;
           count = 1;
           return 0;
       }
       return 0;
   }
   public void findAll()
   {
     
   }
   private void replace()
   {
       mf.getMainText().replaceSelection(replacetext.getText());
   }
   private void replaceAll()
   {
       
   }
   public void showFindAndReplace(MainFrame mf,String title)
   {
       if(title=="查找"||title=="查找下一个")
       {
           this.replacetext.setEnabled(false);
           this.replace.setEnabled(false);
           this.replaceall.setEnabled(false);
       }
       this.setTitle(title);
       this.init();
       this.setLocationRelativeTo(mf);
       this.setAlwaysOnTop(true);
       this.setVisible(true);
   }
    public static void main(String[] args) 
    {
    }
}


最近下载更多
queueandstack  LV3 2022年12月2日
沙发床上的  LV1 2022年5月30日
183077946781  LV1 2021年11月15日
尹恒yingying  LV18 2021年10月12日
任天晴  LV1 2021年7月10日
ncy123456789  LV6 2021年7月6日
毫无qiushengyu  LV2 2021年7月2日
祁康伟  LV1 2021年7月1日
周星星哥哥在外面  LV1 2021年6月15日
lixiaoya  LV1 2021年6月7日
最近浏览更多
ruo12138  LV1 4月11日
潘潘123456  LV2 3月2日
lsglsg9  LV22 2023年12月29日
lshlsh 2023年12月25日
暂无贡献等级
sunshine9920  LV12 2023年10月20日
卢本伟不开挂  LV4 2023年9月3日
taoshen95  LV14 2023年7月27日
zhoulinghhhh 2023年7月18日
暂无贡献等级
dongzhan  LV12 2023年6月30日
新工艺  LV7 2023年6月13日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友