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

📄 notepad_change.java

📁 java 编写的代码
💻 JAVA
字号:
package com.edu.sccp.snail.notepad.view;


import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

 class Notepad_Change extends JFrame implements ActionListener{

	/**
	 * Launch the application
	 * @param args
	 */
	/*public static void main(String args[]) {
		try {
			Notepad_Change frame = new Notepad_Change();
			} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	/**
	 * Create the frame
	 */
	 
	private Notepad_newFile textpad;
	//private Notepad_Main notepad_main = new Notepad_Main();
	private JTextField check_textField = new JTextField();
	private JTextField change_textField = new JTextField();
	private JButton button_CheckNext = new JButton();
	private JButton button_Change = new JButton();
	private JButton button_ChangeAll = new JButton();
	private JButton button_Exit = new JButton();	
	private JCheckBox checkBox = new JCheckBox();
	
	private int FindPos=0,a=0,b=0;	
	
	/*public TextPad getTextpad() {
		return textpad;
	}

	public void setTextpad(TextPad textpad) {
		this.textpad = textpad;
	}*/
	public Notepad_Change(Notepad_newFile nf) {
		super();
		setTitle("替换");
		setResizable(false);
		getContentPane().setLayout(null);
		setBounds(200, 200, 350, 170);		
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        textpad = nf;
        
		final JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBounds(0, 0, 343, 166);
		getContentPane().add(panel);

		final JLabel label = new JLabel();
		label.setFont(new Font("", Font.PLAIN, 12));
		label.setText("查找内容(N)");
		label.setBounds(23, 10, 79, 16);
		panel.add(label);

		final JLabel label_1 = new JLabel();
		label_1.setFont(new Font("", Font.PLAIN, 12));
		label_1.setText("替换为(P)");
		label_1.setBounds(23, 38, 79, 16);
		panel.add(label_1);

		
		check_textField.setBounds(108, 8, 106, 21);		
		panel.add(check_textField);

		
		change_textField.setBounds(108, 36, 106, 21);
		panel.add(change_textField);

		
		button_CheckNext.setFont(new Font("", Font.PLAIN, 11));
		button_CheckNext.setText("查找下一个(F)");
		button_CheckNext.setBounds(230, 6, 103, 25);
		button_CheckNext.addActionListener(this);
		panel.add(button_CheckNext);

		
		button_Change.setFont(new Font("", Font.PLAIN, 12));
		button_Change.setText("替换(R)");
		button_Change.setBounds(230, 33, 103, 25);
		button_Change.addActionListener(this);
		panel.add(button_Change);

		
		button_ChangeAll.setFont(new Font("", Font.PLAIN, 12));
		button_ChangeAll.setText("全部替换(A)");
		button_ChangeAll.setBounds(230, 59, 103, 25);
		button_ChangeAll.addActionListener(this);
		panel.add(button_ChangeAll);

		
		button_Exit.setFont(new Font("", Font.PLAIN, 12));
		button_Exit.setText("取消");
		button_Exit.setBounds(230, 101, 103, 25);
		button_Exit.addActionListener(this);
		panel.add(button_Exit);

		
		checkBox.setFont(new Font("", Font.PLAIN, 12));
		checkBox.setText("区分大小写");
		checkBox.setSelected(true);
		checkBox.setBounds(23, 101, 94, 25);
		panel.add(checkBox);
		this.addWindowListener(new WindowAdapter(){
			public void windowClosed(WindowEvent e){
				setVisible(false);
			}		
		});
		
		this.setVisible(true);
	}

	private void notepad_void_change(){//查找区分大小写
		String strA = textpad.jta.getText(); 
		String strB = check_textField.getText();
				if(a >= 0) { 
					a=strA.indexOf(strB,FindPos); 
					b=strB.length(); 
					FindPos = a + b; 
					if(a == -1) { 
						JOptionPane.showMessageDialog(null, "没有您要查找的信息", "查找结果",1); 
						a = 0; 
						FindPos=0; 
					} 
					textpad.jta.select(a,FindPos); 						
				}	
	}
	private void notepad_void_change_ignore(){//查找不区分大小写
		String strA = textpad.jta.getText(); 
		String strB = check_textField.getText();
		strA = strA.toLowerCase();
		strB = strB.toLowerCase();
				if(a >= 0) { 
					a=strA.indexOf(strB,FindPos); 
					b=strB.length(); 
					FindPos = a + b; 
					if(a == -1) { 
						JOptionPane.showMessageDialog(null, "没有您要查找的信息", "查找结果",1); 
						a = 0; 
						FindPos=0; 
					} 
					textpad.jta.select(a,FindPos); 						
				}	
	}
	public void actionPerformed(ActionEvent e) {		
		if(e.getActionCommand().equals("查找下一个(F)")) {
			if(checkBox.isSelected())
				notepad_void_change();
			else
				notepad_void_change_ignore();
			
		}
		if(e.getActionCommand().equals("替换(R)")){
			String replace = change_textField.getText();
			textpad.jta.replaceSelection(replace);
		}
		if(e.getActionCommand().equals("全部替换(A)")){			
			while(a >= 0){
				String strA = textpad.jta.getText();
				String strB = check_textField.getText();
				String strC = change_textField.getText();
				
				a = strA.indexOf(strB,FindPos);
				if(a == -1) break;				
				b = strB.length();				
				FindPos = a + b;
				textpad.jta.select(a,FindPos);
				String replaceAll = change_textField.getText();
				textpad.jta.replaceSelection(strC);	
			}			
			a = 0 ;
			FindPos = 0;			
		}	
		if(e.getActionCommand().equals("取消")){		
			this.setVisible(false);
		}
	}

	
}

⌨️ 快捷键说明

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