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

<< 返回
  

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

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class Find extends JDialog {
	private Find myself = null;
	private Notepad notepad = null;
	private JTextField text = null;
	private JLabel label = null;
	private JButton findNext = null;
	private JButton cancel = null;
	private JRadioButton up = null;
	private JRadioButton down = null;
	private JPanel panel_Group = null;
	private ButtonGroup group = null;
	private JCheckBox care = null;
	private JPanel panel_Care = null;
	
	public Find(Notepad notepad, boolean visiable) {
		this.myself = this;
		this.setLayout(null);
		this.notepad = notepad;
		this.getReady();
		this.addEvent();
		if (notepad.getTextArea().getSelectedText() != null && !("".equals(notepad.getTextArea().getSelectedText()))) {
			//如果当前有选择的内容,就自动填充为选择的内容
			this.text.setText(notepad.getTextArea().getSelectedText());
			this.findNext.setEnabled(true);
		} else if(notepad.getFindStr() != null && !("".equals(notepad.getFindStr()))) {
				this.text.setText(notepad.getFindStr());
				this.findNext.setEnabled(true);
		} else {
			this.findNext.setEnabled(false);
		}

		if ("UP".equals(notepad.getUpOrDown())) {
			this.up.setSelected(true);
		} else {
			this.down.setSelected(true);
		}
		if (notepad.getIsCare()) {
			this.care.setSelected(true);
		} else {
			this.care.setSelected(false);
		}
		this.setVisible(visiable);
	}
	
	private void getReady() {
		this.label = new JLabel("查找内容:");
		this.label.setSize(100, 25);
		this.label.setLocation(10, 5);
		this.label.setFont(new Font("宋体", 0, 12));
		
		this.text = new JTextField();
		this.text.setSize(170, 25);
		this.text.setLocation(100, 5);
		this.text.setFont(new Font("宋体", 0, 14));
		
		this.findNext = new JButton("查找下一个");
		this.findNext.setSize(100, 25);
		this.findNext.setLocation(280, 5);
		
		this.cancel = new JButton("取消");
		this.cancel.setSize(100, 25);
		this.cancel.setLocation(280, 35);
		
		this.up = new JRadioButton("向上");
		this.down = new JRadioButton("向下");
		this.group = new ButtonGroup();
		this.group.add(this.up);
		this.group.add(this.down);
		this.panel_Group = new JPanel();
		this.panel_Group.setBorder(BorderFactory.createTitledBorder("方向"));
		this.panel_Group.setSize(150, 60);
		this.panel_Group.setLocation(120, 40);
		this.panel_Group.add(this.up);
		this.panel_Group.add(this.down);
		
		this.care = new JCheckBox("区分大小写");
		this.panel_Care = new JPanel();
		this.panel_Care.add(this.care);
		this.panel_Care.setSize(80, 50);
		this.panel_Care.setLocation(10, 70);
		
		this.setSize(400, 130);
		this.setLocation(400,300);
		this.setResizable(false);
		this.setTitle("查找");
		
		this.add(this.label);
		this.add(this.text);
		this.add(this.findNext);
		this.add(this.cancel);
		this.add(this.panel_Group);
		this.add(this.panel_Care);
		
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}
	
	public void autoFindNext() {
		findNext(text.getText(), notepad.getTextArea().getCaretPosition(), notepad.getUpOrDown(), notepad.getIsCare());
	}
	
	private void addEvent() {
		this.cancel.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						myself.setVisible(false);
					}
				});
		this.text.addKeyListener(new TextKeyListener(this));
		this.text.addMouseListener(new TextMouseListener(this));
		this.findNext.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						findNext(text.getText(), notepad.getTextArea().getCaretPosition(), notepad.getUpOrDown(), notepad.getIsCare());
							//当前光标位置向下查找,区分大小写
						notepad.setFindStr(text.getText());
					}
				});
		this.care.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						if (care.isSelected()) {
							notepad.setIsCare(true);
						} else {
							notepad.setIsCare(false);
						}
					}
				});
		this.up.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						if (up.isSelected()) {
							notepad.setUpOrDown("UP");
						} else {
							notepad.setUpOrDown("DOWN");
						}
					}
				});
		this.down.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						if (down.isSelected()) {
							notepad.setUpOrDown("DOWN");
						} else {
							notepad.setUpOrDown("UP");
						}
					}
				});
	}
	
	public void findNext(
			String str, 		// 要查询的目标字符串
			int currentIndex, 	// 当前的光标位置
			String upOrDown, 	// 查询的方向 - 向上 / 向下
			boolean isCare		// 是否区别大小写
			) {
		
		if (str == null || "".equals(str) || 
				currentIndex < 0 || currentIndex > notepad.getTextArea().getText().length() || 
				upOrDown == null || (!("UP".equals(upOrDown)) && !("DOWN".equals(upOrDown)))) {
			System.out.println("异常!\nFind.java: findNext()");
			return;
		}
		if (isCare) {	// 如果区分大小写
			if ("UP".equals(upOrDown)) {	//如果是向上查找
				System.out.println("当前位置向上查找,区分大小写");	// 【已测试】
				if (notepad.getTextArea().getSelectionEnd() == currentIndex
						&& notepad.getTextArea().getSelectionStart() == (currentIndex - str.length())) {
					currentIndex -= (str.length() + 1);
				}
				notepad.getTextArea().select(notepad.getTextArea().getText().lastIndexOf(str, currentIndex), 
						notepad.getTextArea().getText().lastIndexOf(str, currentIndex) + str.length());
			} else {	//如果是向下查找
				System.out.println("当前位置向下查找,区分大小写");	// 【已测试】
				notepad.getTextArea().setSelectionStart(notepad.getTextArea().getText().indexOf(str, currentIndex));
				notepad.getTextArea().setSelectionEnd(notepad.getTextArea().getText().indexOf(str, currentIndex) + str.length());
			}
		} else {		// 如果不区分大小写
			if ("UP".equals(upOrDown)) {	//如果是向上查找
				System.out.println("当前位置向上查找,不区分大小写");
				if (notepad.getTextArea().getSelectionEnd() == currentIndex
						&& notepad.getTextArea().getSelectionStart() == (currentIndex - str.length())) {
					currentIndex -= (str.length() + 1);	// 这里要多一个1,否则死循环
				}
				notepad.getTextArea().select(notepad.getTextArea().getText().toUpperCase().lastIndexOf(str.toUpperCase(), currentIndex), 
						notepad.getTextArea().getText().toUpperCase().lastIndexOf(str.toUpperCase(), currentIndex) + str.length());
			} else {	//如果是向下查找
				System.out.println("当前位置向下查找,不区分大小写");	// 【已测试】
				notepad.getTextArea().setSelectionStart(notepad.getTextArea().getText().toUpperCase().indexOf(str.toUpperCase(), currentIndex));
				notepad.getTextArea().setSelectionEnd(notepad.getTextArea().getText().toUpperCase().indexOf(str.toUpperCase(), currentIndex) + str.length());
			}
		}
	}
	
	public JButton getButton() {
		return this.findNext;
	}
	
	public JTextField getTextField() {
		return this.text;
	}
}

  
<< 返回

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值