if(k>-1) {
//String
strData=strA.subString(k,strB.getText().length()+1);
} else {
JOptionPane.showMessageDialog(null,\找不到您查找的editArea.setCaretPosition(k); editArea.select(k,k+strB.length());
内容!\查找\
}
else if(downButton.isSelected()) {
if(editArea.getSelectedText()==null)
k=strA.indexOf(strB,editArea.getCaretPosition()+1); }
else
k=strA.indexOf(strB,
editArea.getCaretPosition()-findText.getText().length()+1);
if(k>-1) {
//String
strData=strA.subString(k,strB.getText().length()+1);
} else {
JOptionPane.showMessageDialog(null,\找不到您查找的editArea.setCaretPosition(k); editArea.select(k,k+strB.length());
内容!\查找\
}
}
}
27
});//\查找下一个\按钮监听结束 //创建\查找\对话框的界面 JPanel panel1=new JPanel(); JPanel panel2=new JPanel(); JPanel panel3=new JPanel(); JPanel directionPanel=new JPanel();
directionPanel.setBorder(BorderFactory.createTitledBorder(\方向\//设置directionPanel组件的边框;
//BorderFactory.createTitledBorder(String title)创建一个新标题边框,使用
默认边框(浮雕化)、默认文本位置(位于顶线上)、默认调整 (leading) 以及由当前外观确定的默认字体和文本颜色,并指定了标题文本。
directionPanel.add(upButton); directionPanel.add(downButton); panel1.setLayout(new GridLayout(2,1)); panel1.add(findNextButton); panel1.add(cancel);
panel2.add(findContentLabel); panel2.add(findText); panel2.add(panel1);
panel3.add(matchCheckBox); panel3.add(directionPanel); con.add(panel2); con.add(panel3);
findDialog.setSize(410,180);
findDialog.setResizable(false);//不可调整大小 findDialog.setLocation(230,280); findDialog.setVisible(true);
}//查找方法结束
28
//替换方法 public void replace() {
final JDialog replaceDialog=new JDialog(this,\替换\时允许其
他窗口同时处于激活状态(即无模式)
Container con=replaceDialog.getContentPane();//返回此对话框的
contentPane对象
con.setLayout(new FlowLayout(FlowLayout.CENTER)); JLabel findContentLabel=new JLabel(\查找内容(N):\final JTextField findText=new JTextField(15);
JButton findNextButton=new JButton(\查找下一个(F):\JLabel replaceLabel=new JLabel(\替换为(P):\final JTextField replaceText=new JTextField(15); JButton replaceButton=new JButton(\替换(R)\JButton replaceAllButton=new JButton(\全部替换(A)\JButton cancel=new JButton(\取消\cancel.addActionListener(new ActionListener() { });
final JCheckBox matchCheckBox=new JCheckBox(\区分大小写(C)\ButtonGroup bGroup=new ButtonGroup();
final JRadioButton upButton=new JRadioButton(\向上(U)\final JRadioButton downButton=new JRadioButton(\向下(U)\downButton.setSelected(true); bGroup.add(upButton); bGroup.add(downButton);
/*ButtonGroup此类用于为一组按钮创建一个多斥(multiple-exclusion)
public void actionPerformed(ActionEvent e) { }
replaceDialog.dispose();
作用域。
29
使用相同的 ButtonGroup 对象创建一组按钮意味着“开启”其中一个按
钮时,将关闭组中的其他所有按钮。*/
/*JRadioButton此类实现一个单选按钮,此按钮项可被选择或取消选择,
并可为用户显示其状态。
与 ButtonGroup 对象配合使用可创建一组按钮,一次只能选择其中的一
个按钮。
(创建一个 ButtonGroup 对象并用其 add 方法将 JRadioButton 对象
包含在此组中。)*/
//\查找下一个\按钮监听
findNextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//\区分大小写(C)\的JCheckBox是否被选中 int k=0,m=0;
final String str1,str2,str3,str4,strA,strB; str1=editArea.getText(); str2=findText.getText(); str3=str1.toUpperCase(); str4=str2.toUpperCase();
if(matchCheckBox.isSelected())//区分大小写 { }
else//不区分大小写,此时把所选内容全部化成大写(或小写),以便
strA=str1; strB=str2;
于查找
{ }
strA=str3; strB=str4;
if(upButton.isSelected()) {
//k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
30
if(editArea.getSelectedText()==null)
k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
else
k=strA.lastIndexOf(strB,
editArea.getCaretPosition()-findText.getText().length()-1);
if(k>-1) {
//String
strData=strA.subString(k,strB.getText().length()+1);
} else {
JOptionPane.showMessageDialog(null,\找不到您查找的editArea.setCaretPosition(k); editArea.select(k,k+strB.length());
内容!\查找\
}
else if(downButton.isSelected()) {
if(editArea.getSelectedText()==null)
k=strA.indexOf(strB,editArea.getCaretPosition()+1); }
else
k=strA.indexOf(strB,
editArea.getCaretPosition()-findText.getText().length()+1);
if(k>-1) {
//String
strData=strA.subString(k,strB.getText().length()+1);
} else
editArea.setCaretPosition(k); editArea.select(k,k+strB.length());
31
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库JAVA记事本(7)在线全文阅读。
相关推荐: