📄 replace.java
字号:
package note;
import java.awt.event.ActionEvent;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
/*
* Replace.java
*
* Created on 2008年4月23日, 下午12:27
*/
/**
*
* @author Xium
*/
public class Replace extends javax.swing.JFrame {
/**
*
*/
private static final long serialVersionUID = 5638690041341638551L;
/** Creates new form Replace */
findDialog fd;
public Replace() {
initComponents();
}
private void initComponents() {
jButton_FindNext = new JButton();
jCheckBox_Capital = new JCheckBox();
jTextField_Find = new JTextField();
jTextField_Replace = new JTextField();
jLabel_Replace_findText = new JLabel();
jLabel_Replace_replaceText = new JLabel();
jButton_Replace = new JButton();
jButton_ReplaceAll = new JButton();
jButton_Replace_Cancel = new JButton();
setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE);
setBackground(new java.awt.Color(204, 204, 255));
jButton_FindNext.setText("查找下一个");
jButton_FindNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_FindNextActionPerformed(evt);
}
});
jCheckBox_Capital.setText("区分大小写");
jCheckBox_Capital.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox_CapitalActionPerformed(evt);
}
});
jTextField_Find.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField_FindActionPerformed(evt);
}
});
jLabel_Replace_findText.setText("查找内容:");
jLabel_Replace_replaceText.setText("替换为:");
jButton_Replace.setText("替 换");
jButton_Replace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_ReplaceActionPerformed(evt);
}
});
jButton_ReplaceAll.setText("全部替换");
jButton_ReplaceAll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_ReplaceAllActionPerformed(evt);
}
});
jButton_Replace_Cancel.setText("取 消");
jButton_Replace_Cancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_Replace_CancelActionPerformed(evt);
}
});
jTextField_Find.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent evt) {
jTextCaretUpdate(evt);
}
});
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup( GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
.addComponent(jLabel_Replace_findText, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel_Replace_replaceText, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE))
.addPreferredGap( LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
.addComponent(jTextField_Find, GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
.addComponent(jTextField_Replace, GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE))
.addPreferredGap( LayoutStyle.ComponentPlacement.RELATED, 18, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jCheckBox_Capital, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
.addPreferredGap( LayoutStyle.ComponentPlacement.RELATED)))
.addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
.addComponent(jButton_Replace, GroupLayout.Alignment.TRAILING)
.addComponent(jButton_FindNext, GroupLayout.Alignment.TRAILING)
.addComponent(jButton_ReplaceAll, GroupLayout.Alignment.TRAILING)
.addComponent(jButton_Replace_Cancel, GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
layout.linkSize( SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton_Replace, jButton_FindNext, jButton_ReplaceAll, jButton_Replace_Cancel});
layout.setVerticalGroup(
layout.createParallelGroup( GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(jButton_FindNext, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jTextField_Find, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel_Replace_findText, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE))
.addPreferredGap( LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(1, 1, 1)
.addGroup(layout.createParallelGroup( GroupLayout.Alignment.BASELINE)
.addComponent(jLabel_Replace_replaceText, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField_Replace, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton_Replace, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(3, 3, 3)))
.addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(jButton_ReplaceAll, GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
.addPreferredGap( LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton_Replace_Cancel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jCheckBox_Capital, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.linkSize( SwingConstants.VERTICAL, new java.awt.Component[] {jLabel_Replace_findText, jLabel_Replace_replaceText, jTextField_Find, jTextField_Replace});
pack();
}// </editor-fold>
protected void jTextCaretUpdate(CaretEvent evt) {
jButton_Replace.setEnabled(true);
jButton_FindNext.setFocusable(true);
}
/*这个替换的方法是先查找到要替换的内容(查找方法与findDialog 的查找相同)
* 用int info 作参数 传递是对话框中的具体按键的事件
* 当 info = 0 时 是实现替换对话框中的查找()功能
* 当 info = 1 时 是实现的替换功能 他的每一次事件相应 是由查找和替换两步完成
*
* 没有弄明白 replace 方法怎么用
* 这里的替换是用 在Stringbuffer 先删除 后插入的方式来完成
* 每次替换都会实现一次 setText 所以当 使用撤销的时候 会 有空白
*
* 没有想到如何区分 查找(替换完毕) 和 查找(没有找到)的办法
* 查找完毕或者是没有找到 就用了相同的 (找不到的)反馈信息
* */
protected void find (int info) {
String str_find = jTextField_Find.getText().trim();//从查找框中获得内容
int str_find_long = str_find.length();//计算文本内容的长度 ;
String str_fileArea = Note.fileArea.getText();
String str_up_find; //设置为向上查找的字符串
int str_down_index;//向下替换的初始值
String str_replace = jTextField_Replace.getText().trim();
int str_replace_long = str_replace.length();
StringBuffer strbuf_fileArea = new StringBuffer(Note.fileArea.getText());
int str_find_index ;
if(jCheckBox_Capital.isSelected()) { //是否区分大小写. 是就执行
str_down_index=Note.fileArea.getCaretPosition();
str_find_index=str_fileArea.indexOf(str_find,str_down_index); //查找字符串出现的位置,如果没找到则返回-1
if(str_find_index==-1)
{
JOptionPane.showMessageDialog(null,"找不到“ "+str_find+" ”(替换完毕)","记事本",JOptionPane.QUESTION_MESSAGE);
jTextField_Find.selectAll();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -