?? replaceframe.java
字號(hào):
import java.awt.datatransfer.*;
import java.io.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
class ReplaceFrame extends Frame implements ActionListener,ItemListener
{
Panel r,r_1,r_2,v_1,v_2,r_3;
Button findButton,cancel,replace,replaceall;
Box baseBox,H1,H2,V1,H3,V2;
Label content,replaceas;
TextField text;
TextField replacetext;
Checkbox type,up,down;
Image im;
static int end=0;
static int tempstart=0;
static int start=0;
String tempstring="";
static int flagsend=0;
ReplaceFrame()
{
super("替換對(duì)話框");
Color c=new Color(236,233,216);
this.setBackground(c);
im=Toolkit.getDefaultToolkit().getImage("d:\\1.1.jpg");
this.setIconImage(im);
H1=Box.createHorizontalBox();
content=new Label("查找內(nèi)容");
text=new TextField(11);
H1.add(content);
H1.add(Box.createHorizontalStrut(1));
H1.add(text);
replacetext=new TextField(10);
replaceas=new Label("替換為 ");
H2=Box.createHorizontalBox();
H2.add(replaceas);
H2.add(Box.createHorizontalStrut(1));
H2.add(replacetext);
H3=Box.createHorizontalBox();
findButton=new Button("查找下一個(gè)");
replace=new Button("替換");
replace.addActionListener(this);
replaceall=new Button("替換所有");
replaceall.addActionListener(this);
cancel=new Button("取消");
findButton.addActionListener(this);
cancel.addActionListener(this);
r_3=new Panel();
r_3.setLayout(new GridLayout(4,1));
r_3.add(findButton);
r_3.add(replace);
r_3.add(replaceall);
r_3.add(cancel);
V1=Box.createVerticalBox();
V1.add(H1);
V1.add(H2);
type=new Checkbox("區(qū)分大小寫");
type.addItemListener(this);
V1.add(type);
r=new Panel();
r.setLayout(new BorderLayout());
r.add(V1,BorderLayout.CENTER);
r.add(r_3,BorderLayout.EAST);
add(r,BorderLayout.CENTER);
this.setVisible(true);
this.setBounds(300,300,330,120);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
public void actionPerformed(ActionEvent e1)
{
String s=text.getText();
String string=MyFrame.text.getText();
if(e1.getSource()==findButton)
{
System.out.println("運(yùn)算前的start:"+start);
System.out.println("運(yùn)算前的end:"+end);
if(text.getText()!=null )
{
if(end<=string.length()-1)
{
if(type.getState()==true)
{
start=tempstring.indexOf(s,tempstart);
}
else
{
start=string.indexOf(s,tempstart);
}
end=start+s.length();
System.out.println("運(yùn)算后的start:"+start);
System.out.println("運(yùn)算后的end:"+end);
MyFrame.text.setSelectionStart(start);
MyFrame.text.setSelectionEnd(end);
if((end)<=string.length()-1)
{
tempstart=end;
}
System.out.println("運(yùn)算后的start:"+start);
System.out.println("tempstart:"+tempstart);
}
}
}
if(e1.getSource()==replace)
{
if(text.getText()!=null && replacetext.getText()!=null)
{
if(end<string.length())
{
int start=string.indexOf(s,tempstart);
end=start+s.length();
System.out.println("運(yùn)算前的start:"+start);
System.out.println("運(yùn)算前的end:"+end);
MyFrame.text.setSelectionStart(start);
MyFrame.text.setSelectionEnd(end);
MyFrame.text.replaceRange(replacetext.getText(),start,end);
if(end<=string.length()-1)
{
tempstart=end;
}
if(end>string.length()-1)
{
tempstart=end;
}
System.out.println("運(yùn)算后的start:"+start);
}
}
}
if(e1.getSource()==replaceall)
{
try
{
if(text.getText()!=null && replacetext.getText()!=null)
{
while(end<string.length())
{
int start=string.indexOf(s,tempstart);
end=start+s.length();
MyFrame.text.setSelectionStart(start);
MyFrame.text.setSelectionEnd(end);
MyFrame.text.replaceRange(replacetext.getText(),start,end);
if((end)<=string.length()-1)
{
tempstart=end;
}
}
if(end>string.length()-1)
{
tempstart=0;
end=0;
}
flagsend=1;
}
}
catch(Exception e)
{
}
}
if(e1.getSource()==cancel)
{
dispose();
}
}
public void itemStateChanged(ItemEvent ie)
{
String s=text.getText();
String string=MyFrame.text.getText();
String ups=s.toUpperCase();
String lows=s.toLowerCase();
if(ie.getItemSelectable()==type)
{
if(s.equals(ups))
{
tempstring=string.toUpperCase();
}
if(s.equals(lows))
{
tempstring=string.toLowerCase();
}
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -