引用页_初学Java:仿写记事本_Replace.java

<< 返回
  

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class Replace extends JDialog {
	private Replace myself = null;
	private Notepad notepad = null;
	private JTextField textFrom = null;
	private JLabel labelFrom = null;
	private JButton findNext = null;
	private JTextField textTo = null;
	private JLabel labelTo = null;
	private JButton cancel = null;
	private JCheckBox care = null;
	private JPanel panel_Care = null;
	private JButton replace = null;
	private JButton replaceAll = null;
	private boolean isCare = false;
	
	public Replace(Notepad notepad) {
		this.myself = this;
		this.notepad = notepad;
		this.setLayout(null);
		this.getReady();
		this.addEvent();
		this.fillText();
		this.setVisible(true);
	}
	
	private void getReady() {
		this.labelFrom = new JLabel("查找内容:");
		this.labelFrom.setSize(100, 25);
		this.labelFrom.setLocation(10, 5);
		this.labelFrom.setFont(new Font("宋体", 0, 12));
		
		this.labelTo = new JLabel("替换为:");
		this.labelTo.setSize(100, 25);
		this.labelTo.setLocation(10, 35);
		this.labelTo.setFont(new Font("宋体", 0, 12));
		
		this.textFrom = new JTextField();
		this.textFrom.setSize(170, 25);
		this.textFrom.setLocation(100, 5);
		this.textFrom.setFont(new Font("宋体", 0, 12));
		
		this.textTo = new JTextField();
		this.textTo.setSize(170, 25);
		this.textTo.setLocation(100, 35);
		this.textTo.setFont(new Font("宋体", 0, 12));
		
		this.findNext = new JButton("查找下一个");
		this.findNext.setSize(100, 25);
		this.findNext.setLocation(280, 5);
		this.findNext.setEnabled(false);
		
		this.replace = new JButton("替换");
		this.replace.setSize(100, 25);
		this.replace.setLocation(280, 35);
		this.replace.setEnabled(false);
		
		this.replaceAll = new JButton("全部替换");
		this.replaceAll.setSize(100, 25);
		this.replaceAll.setLocation(280, 65);
		this.replaceAll.setEnabled(false);
		
		this.cancel = new JButton("取消");
		this.cancel.setSize(100, 25);
		this.cancel.setLocation(280, 95);
		
		this.care = new JCheckBox("区分大小写");
		this.care.setSelected(false);
		this.panel_Care = new JPanel();
		this.panel_Care.add(this.care);
		this.panel_Care.setSize(80, 50);
		this.panel_Care.setLocation(10, 95);
		
		this.setSize(400, 160);
		this.setLocation(400, 300);
		this.setResizable(false);
		this.setTitle("替换");
		
		this.add(this.labelFrom);
		this.add(this.labelTo);
		this.add(this.textTo);
		this.add(this.textFrom);
		this.add(this.findNext);
		this.add(this.replace);
		this.add(this.replaceAll);
		this.add(this.cancel);
		this.add(this.panel_Care);
		
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}
	
	private void addEvent() {
		this.textFrom.addKeyListener(new TextKeyListener(this));
		this.textFrom.addMouseListener(new TextMouseListener(this));
		this.care.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						myself.isCare = care.isSelected();
					}
				});
		this.findNext.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						findNext(textFrom.getText(), notepad.getTextArea().getCaretPosition(), isCare);
						notepad.setFindStr(textFrom.getText());
					}
				});
		this.replace.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						if (notepad.getTextArea().getSelectedText() == null || "".equals(notepad.getTextArea().getSelectedText())) {
							findNext(textFrom.getText(), notepad.getTextArea().getCaretPosition(), isCare);
							return;
						} else {
							if (!notepad.getTextArea().getSelectedText().equals(textFrom.getText())) {
								// 如果当前选中的和textFrom中的内容不一样,就不能把它替换掉,要当前位置重新查找
								findNext(textFrom.getText(), notepad.getTextArea().getCaretPosition(), isCare);
								return;
							}
						}
						if (textTo.getText() == null) textTo.setText("");	// 防止空指向异常
						notepad.getTextArea().replaceSelection(textTo.getText());	// 替换完成
						findNext(textFrom.getText(), notepad.getTextArea().getCaretPosition(), isCare);
						notepad.setFindStr(textFrom.getText());
						notepad.setReplaceStr(textTo.getText());
					}
				});
		this.replaceAll.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						if (textTo.getText() == null) textTo.setText("");	// 防止空指向异常
						notepad.getTextArea().setCaretPosition(0);
						findNext(textFrom.getText(), notepad.getTextArea().getCaretPosition(), isCare);
						while (true) {
							if (!notepad.getTextArea().getSelectedText().equals(textFrom.getText())) return;
								// 如果没有这句,会造成死循环,我没时间去看为什么,直接规避吧,嗯……
							System.out.println("选中了:[" + notepad.getTextArea().getSelectedText() + "]");
							if (notepad.getTextArea().getSelectedText() == null || "".equals(notepad.getTextArea().getSelectedText())) return;
							
							notepad.getTextArea().replaceSelection(textTo.getText());	// 替换完成
							findNext(textFrom.getText(), notepad.getTextArea().getCaretPosition(), isCare);
							if (notepad.getTextArea().getCaretPosition() >= notepad.getTextArea().getText().length()) {
								System.out.println("退出循环");
								return;
							}
						}
					}
				});
		this.cancel.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						myself.dispose();
					}
				});
	}
	
	private void findNext(String str, int currentIndex, boolean isCare) {
		if (str == null || "".equals(str) || currentIndex < 0 || currentIndex > notepad.getTextArea().getText().length()) {
			System.err.println("异常!\nFind.java: findNext()");
			return;
		}
		if (isCare) {	// 如果区分大小写
			notepad.getTextArea().setSelectionStart(notepad.getTextArea().getText().indexOf(str, currentIndex));
			notepad.getTextArea().setSelectionEnd(notepad.getTextArea().getText().indexOf(str, currentIndex) + str.length());
		} else {		// 如果不区分大小写
			notepad.getTextArea().setSelectionStart(notepad.getTextArea().getText().toUpperCase().indexOf(str.toUpperCase(), currentIndex));
			notepad.getTextArea().setSelectionEnd(notepad.getTextArea().getText().toUpperCase().indexOf(str.toUpperCase(), currentIndex) + str.length());
		}
	}
	
	private void fillText() {
		if (this.notepad.getTextArea().getSelectedText() != null
				&& !("".equals(this.notepad.getTextArea().getSelectedText()))) {
			this.textFrom.setText(this.notepad.getTextArea().getSelectedText());
		} else if (this.notepad.getFindStr() != null
				&& !("".equals(this.notepad.getFindStr()))) {
			this.textFrom.setText(this.notepad.getFindStr());
		} 
	}
	
	public JTextField getTextField() {
		return this.textFrom;
	}
	
	public JButton getButton(int num) {
		if (num == 1) return this.findNext;
		if (num == 2) return this.replace;
		if (num == 3) return this.replaceAll;
		return null;
	}
}

  
<< 返回

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值