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

📄 replacedialog.java

📁 用java写的记事本 基本功能与windows自带的相同
💻 JAVA
字号:
package notebook;



import java.awt.BorderLayout;
import java.awt.Frame;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.undo.*;
import java.awt.*;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Button;
import java.awt.Panel;
import java.awt.FlowLayout;
import java.awt.SystemColor;

import com.borland.jbcl.layout.*;
import java.awt.Label;
import java.awt.TextArea;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.CheckboxGroup;
import javax.swing.ButtonGroup;


/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class ReplaceDialog extends JDialog implements ActionListener{
    JPanel panel1 = new JPanel();
    XYLayout xYLayout1 = new XYLayout();
    Button btnFind = new Button();
    JCheckBox jcbMatchCase = new JCheckBox();
    JTextField jtxtFind = new JTextField();
    Label label1 = new Label();
    Label label2 = new Label();
    Button btnReplace = new Button();
    JTextField jtxtReplace = new JTextField();
    Button btnReplaceAll = new Button();
    Button btnCancel = new Button();

    NoteBook mainFrame = null;//主窗口
     int FindStartPos = 0; //查找初始位置
    public ReplaceDialog(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        try {
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            jbInit();
            pack();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    public ReplaceDialog(Frame parent) {
        super(parent);
        try {
            mainFrame = (NoteBook)parent;
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            jbInit();
            pack();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }


    public ReplaceDialog() {
        this(new Frame(), "MainFrame_ReplaceBox", false);
    }

    private void jbInit() throws Exception {
        panel1.setLayout(xYLayout1);
        btnFind.setLabel("查找下一个");
        jcbMatchCase.setText("区分大小写");
        label1.setText("查找内容(N):");
        panel1.setPreferredSize(new Dimension(400, 135));
        label2.setText("替换为(P):");
        btnReplace.setLabel("替换");
        btnReplaceAll.setLabel("全部替换");
        btnCancel.setLabel("取消");
        this.setTitle("替换");
        getContentPane().add(panel1);
        panel1.add(jtxtReplace, new XYConstraints(116, 44, 152, -1));
        panel1.add(label2, new XYConstraints(21, 48, -1, 13));
        panel1.add(label1, new XYConstraints(21, 17, -1, -1));
        panel1.add(jtxtFind, new XYConstraints(115, 15, 152, -1));
        panel1.add(jcbMatchCase, new XYConstraints(21, 98, -1, -1));
        panel1.add(btnCancel, new XYConstraints(281, 100, 95, -1));
        panel1.add(btnReplaceAll, new XYConstraints(281, 69, 95, -1));
        panel1.add(btnReplace, new XYConstraints(281, 39, 95, -1));
        panel1.add(btnFind, new XYConstraints(281, 9, 95, -1));
        panel1.setBackground(SystemColor.control);
        jcbMatchCase.setBackground(SystemColor.control);
        btnCancel.addActionListener(this);
        btnFind.addActionListener(this);
        btnReplace.addActionListener(this);
        btnReplaceAll.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==btnCancel)
        {
            this.dispose();
        }else if(e.getSource()==btnFind)
        {
           this.findString();
        }else if(e.getSource()==btnReplace)
        {
            if(this.jtxtReplace.getText().length()==0 && mainFrame.textArea.getSelectedText() !=null)
            {
                mainFrame.textArea.replaceSelection("");
            }else if(this.jtxtReplace.getText().length()>0 && mainFrame.textArea.getSelectedText() !=null)
            {
                 mainFrame.textArea.replaceSelection(this.jtxtReplace.getText());
            }
            this.findString();
        }else if(e.getSource()==btnReplaceAll)
        {
            this.replaceAllString();
        }
    }
    //查找字符串方法
    private void findString()
    {
        int a = 0, b = 0;
        String str1,str2,str3,str4,strA,strB;
        str1 = mainFrame.textArea.getText();
        str2 = str1.toLowerCase();
        str3 = this.jtxtFind.getText();//查找的字符串
        str4 = str3.toLowerCase();//转化成小写的查找字符
        mainFrame.strFind=str3;//为主窗口查找字符串赋值
        if (jcbMatchCase.isSelected()==true)
        {
            strA = str1;
            strB = str3;
        }
        else
        {
            strA = str2;
            strB = str4;
        }
        a = strA.indexOf(strB, FindStartPos);
        System.out.println(a);
        if (a > -1)
        {
            mainFrame.textArea.setCaretPosition(a);
            b = jtxtFind.getText().length();
            mainFrame.textArea.select(a, a + b);
            FindStartPos = a + b;
            mainFrame.FindStartPos= FindStartPos;//为主窗口的查找位置赋值
         } else {
            JOptionPane.showMessageDialog(null, "找不到\"" + str3 + "\".","记事本", 1);
            FindStartPos = 0;
        }
    }
    //全部替换字符串方法
    private void replaceAllString()
    {
        int a = 0;
        while(a>-1)
        {
            int b = 0;
            String str1, str2, str3, str4, strA, strB;
            str1 = mainFrame.textArea.getText();
            str2 = str1.toLowerCase();
            str3 = this.jtxtFind.getText(); //查找的字符串
            str4 = str3.toLowerCase(); //转化成小写的查找字符
            mainFrame.strFind = str3; //为主窗口查找字符串赋值
            if (jcbMatchCase.isSelected() == true) {
                strA = str1;
                strB = str3;
            } else {
                strA = str2;
                strB = str4;
            }
            a = strA.indexOf(strB, FindStartPos);
            System.out.println(a);
            if (a > -1) {
                mainFrame.textArea.setCaretPosition(a);
                b = jtxtFind.getText().length();
                mainFrame.textArea.select(a, a + b);
                FindStartPos = a + b;
                mainFrame.FindStartPos = FindStartPos; //为主窗口的查找位置赋值
            }
            else
            {
                FindStartPos = 0;
            }
            if(this.jtxtReplace.getText().length()==0 && mainFrame.textArea.getSelectedText() !=null)
            {
                mainFrame.textArea.replaceSelection("");
            }else if(this.jtxtReplace.getText().length()>0 && mainFrame.textArea.getSelectedText() !=null)
            {
                 mainFrame.textArea.replaceSelection(this.jtxtReplace.getText());
            }

        }
    }

}

⌨️ 快捷键说明

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