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

📄 jfinddialog.java

📁 访windows记事本
💻 JAVA
字号:
//package swjtu.ctb.dialog;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.BadLocationException;

public class JFindDialog extends JDialog {
	JPanel panel;
 	JButton btnFind = new JButton("查找");
 	JButton btnReplace = new JButton("替换");
 	JButton btnCancel = new JButton("关闭");
 	JTextField txtFind = new JTextField(20);
 	JTextField txtReplace = new JTextField(20);
 	JRadioButton btnFindForward = new JRadioButton("向下查找", true);
 	JRadioButton btnFindBackward = new JRadioButton("向上查找", false);
 	ButtonGroup btnGroup = new ButtonGroup();
 	JTextArea findArea;
 
 	public JFindDialog(JFrame owner, JTextArea findArea) {
  		super(owner, "查找", true);
  		this.findArea = findArea;
  
  		//--  设置界面  --//
  		panel = (JPanel)this.getContentPane();
  		JPanel panelCenter = new JPanel();
  		JPanel panelBottom = new JPanel();
  		panel.add(panelCenter, BorderLayout.CENTER);
  		panel.add(panelBottom, BorderLayout.SOUTH);
  		JPanel panel01 = new JPanel();
  		panelCenter.add(panel01);
  		Box box1 = Box.createVerticalBox();
  		panel01.add(box1);
  		box1.add(txtFind);
  		box1.createVerticalStrut(20);
  		box1.add(txtReplace);
  		JPanel panel02 = new JPanel();
  		panelCenter.add(panel02);
  		Box box2 = Box.createVerticalBox();
  		panel02.add(box2);
  		box2.add(btnFind);
  		box1.createVerticalStrut(10);
  		box2.add(btnReplace);
  		box1.createVerticalStrut(10);
  		box2.add(btnCancel);
  		panelBottom.add(btnFindForward);
  		panelBottom.add(btnFindBackward);
  
  		btnGroup.add(btnFindForward);
  		btnGroup.add(btnFindBackward);
  
  		this.getRootPane().setDefaultButton(btnFind);
  		//------------------------//
  
  		btnFind.addActionListener(new ActionListener() {
   		public void actionPerformed(ActionEvent e) {
    			if (!find()) {
     				JOptionPane.showMessageDialog(null, "找不到\"" + txtFind.getText() + "\"", 
               				"IJEdit提示", JOptionPane.INFORMATION_MESSAGE);
    			}
   		}
  		});
  		btnReplace.addActionListener(new ActionListener() {
   		public void actionPerformed(ActionEvent e) {
    			if (!replace()) {
     				JOptionPane.showMessageDialog(null, "找不到\"" + txtFind.getText() + "\"", 
            	   	"IJEdit提示", JOptionPane.INFORMATION_MESSAGE);
    			}
   		}
  		});
  		btnCancel.addActionListener(new ActionListener() {
   		public void actionPerformed(ActionEvent e) {
    			close();
   		}
  		});
  
  		this.pack();
  		this.setLocationRelativeTo(owner);
 	}
 
 	public void showFindDialog() {
  		btnReplace.setVisible(false);
  		txtReplace.setVisible(false);
  		this.setVisible(true);
 	}
 
 	public void showReplaceDialog() {
  		btnReplace.setVisible(true);
  		txtReplace.setVisible(true);
  		this.setVisible(true);
 	}
 
 	private void close() {
  		this.dispose();
 	}
 
 	protected boolean find() {
  		boolean result = false;
  		String findStr = txtFind.getText();
  		int findStrLen = findStr.length();
  		int pos = 0;
	  
  	/* 如果是向下查找,查找起点就是SelectionEnd;
   	  如果是向上查找,查找起点就是SelectionStart - 1。 */
  		if (btnFindForward.isSelected()) {
   		pos = findArea.getSelectionEnd();
  		} else if (btnFindBackward.isSelected()) {
   		pos = findArea.getSelectionStart()-1;
  		}
  
  		if (findStrLen == 0) return result;
  			try {
   			while (pos >= 0 && pos <= findArea.getText().length()) {
    				String temp = findArea.getText(pos, findStrLen);
    				if (temp.compareTo(findStr)==0) {
     					findArea.select(pos, pos + findStrLen);
     					result = true;
     					break;
    				}
    				if (btnFindForward.isSelected()) {
				   	pos++;
    				} else if (btnFindBackward.isSelected()) {
     					pos--;
    				}
   			} // end while 
  			} catch (BadLocationException ble) {
   			result = false;
  			}
  		return result;
 	} // end method
 
 	protected boolean replace() {
  		boolean result = false;
  		String replaceStr = txtReplace.getText();
  		if (find() && replaceStr.length()>0) {
   		findArea.replaceSelection(replaceStr);
   		result = true;
  		}
  		return result;
 	}
}

⌨️ 快捷键说明

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