📄 jreplacedialog.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package jnotepad;import java.awt.*;import java.awt.event.*;import javax.swing.*;/** * * @author Administrator */class JReplaceDialog extends JDialog implements ActionListener { public JReplaceDialog(Frame owner, JTextArea area) { super(owner, "查找", false); this.area = area; //this.setLayout(new FlowLayout()); setLayout(null); //将Layout设为null label.setBounds(10, 10, 80, 25); txtfld.setBounds(90, 10, 250, 25); findbn.setBounds(345, 10, 120, 25); rlabel.setBounds(10, 40, 80, 25); rtxtfld.setBounds(90, 40, 250, 25); upper_lower.setBounds(10, 80, 150, 25); replaceone.setBounds(345, 40, 120, 25); replaceall.setBounds(345, 70, 120, 25); cancelbn.setBounds(345, 100, 120, 25); this.add(label); this.add(txtfld); this.add(findbn); this.add(rlabel); this.add(rtxtfld); this.add(replaceone); this.add(upper_lower); this.add(cancelbn); this.add(replaceall); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { JReplaceDialog.this.dispose();//!!!!!!!! } }); txtfld.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) { String str = txtfld.getText(); if(!str.equals("")) { findbn.setEnabled(true); replaceone.setEnabled(true); replaceall.setEnabled(true); } else { findbn.setEnabled(false); replaceone.setEnabled(false); replaceall.setEnabled(false); } } }); findbn.addActionListener(this); findbn.setEnabled(false); replaceone.addActionListener(this); replaceone.setEnabled(false); replaceall.addActionListener(this); replaceall.setEnabled(false); cancelbn.addActionListener(this); upper_lower.addActionListener(this); this.setResizable(false); this.setBounds(500, 300, 500, 170); this.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == findbn) { findNext(); } if (e.getSource() == cancelbn) { JReplaceDialog.this.dispose(); } if (e.getSource() == upper_lower) { if (teller) { teller = false; } else { teller = true; } } if (e.getSource() == replaceone) { replaceNext(); } if (e.getSource() == replaceall) { replaceAll(); } } private void findNext() { String obj = txtfld.getText(); String all = area.getText(); int len = obj.length(); int end = all.length(); int start = area.getSelectionEnd();//从光标位置开始查找字符串 start = (start == end) ? 0 : start; for (; start <= end - len; start++) { if (teller) { if (all.substring(start, start + len).equals(obj)) {//若找到查找字符串 area.setSelectionStart(start); //选中并显示字符串 area.setSelectionEnd(start + len); return; //退出方法 } } else if (all.substring(start, start + len).toLowerCase().equals(obj.toLowerCase())) {//若找到查找字符串 area.setSelectionStart(start); //选中并显示字符串 area.setSelectionEnd(start + len); return; //退出方法 } } JOptionPane.showMessageDialog(this, " 找不到“" + obj + "”", "记事本", JOptionPane.INFORMATION_MESSAGE); } private void replaceNext() { String oldstr = txtfld.getText(); String newstr = rtxtfld.getText(); String selectedstr = area.getSelectedText(); if(selectedstr!=null && selectedstr.equals(oldstr)) { area.replaceRange(newstr, area.getSelectionStart(), area.getSelectionEnd()); findNext(); } else findNext(); } private void replaceAll() { String oldstr = txtfld.getText(); String newstr = rtxtfld.getText(); String all = area.getText(); int oldlen = oldstr.length(); int newlen = newstr.length(); int dlen = newlen - oldlen; int end = all.length(); int start = 0; try{ for (; start <= end - oldlen;end += dlen, start += newlen) { all = area.getText(); if (teller) { if (all.substring(start, start + oldlen).equals(oldstr)) {//若找到查找字符串 area.replaceRange(newstr, start, start+oldlen); } } else if (all.substring(start, start + oldlen).toLowerCase().equals(oldstr.toLowerCase())) {//若找到查找字符串 area.replaceRange(newstr, start, start+oldlen); } } }catch(Exception ex){} } private JLabel label = new JLabel("查找内容(N):"); private JTextField txtfld = new JTextField(24); private JLabel rlabel = new JLabel("替 换 为(P):"); private JTextField rtxtfld = new JTextField(24); private JButton findbn = new JButton("查找下一个(F)"); private JButton replaceone = new JButton("替 换(R)"); private JButton replaceall = new JButton("全部替换(A)"); private JButton cancelbn = new JButton("取 消"); private JCheckBox upper_lower = new JCheckBox("是否区分大小写(C)"); JTextArea area = new JTextArea(); boolean teller = false;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -