📄 mytxt.java
字号:
}
else if(e.getSource()==ji9||e.getSource()==jp_Cut){
Txt.requestFocus();
String s=Txt.getSelectedText();
Transferable ts=new StringSelection(s);
cb.setContents(ts, null);//要求Transferable对象
Txt.replaceRange("", Txt.getSelectionStart(), Txt.getSelectionEnd());
checkMenuItemEnabled();
}
else if(e.getSource()==ji10||e.getSource()==jp_Copy){
Txt.requestFocus();
String s=Txt.getSelectedText();
Transferable ts=new StringSelection(s);
cb.setContents(ts, null);//要求Transferable对象
checkMenuItemEnabled();
}
else if(e.getSource()==ji11||e.getSource()==jp_Parst){
/*Transferable s=cb.getContents(this);
Txt.insert(s.toString(), Txt.getCaretPosition());
checkMenuItemEnabled();*/
//上面s.toString()并不同与下面的方法,可以试试比较,我试过了...
Txt.requestFocus();
Transferable contents=cb.getContents(this);
if(contents==null) return;
String text;
text="";
try{
text=(String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch(Exception exception){
}
Txt.replaceRange(text,Txt.getSelectionStart(),Txt.getSelectionEnd());
checkMenuItemEnabled();
///别人做的功能......
}
else if(e.getSource()==ji12||e.getSource()==jp_Del){
Txt.requestFocus();
Txt.replaceSelection("");
checkMenuItemEnabled();
}
else if(e.getSource()==ji13){
new MySearch();
}
else if(e.getSource()==ji14){
search();
}
else if(e.getSource()==ji15){
new Myreplace();
}
else if(e.getSource()==ji17||e.getSource()==jp_SelectAll){
Txt.selectAll();
}
else if(e.getSource()==ji18){
Txt.insert(new Date().toString(), Txt.getCaretPosition());
}
else if(e.getSource()==ji20){
new MyFontDialog(MyTxt.this.Txt);
}
else if(e.getSource()==ji22){
String aboutMe=new String("<center><br>点击我找百度<br>"+
"<a href=http://www.baidu.com/>"+
"http://www.baidu.com</a><br><br><br></center>");
JFrame jf=new JFrame("帮助主题");
final JEditorPane jep=new JEditorPane();
jep.setEditable(false);
jep.setContentType("text/html");
jep.setText(aboutMe);
jep.addHyperlinkListener(new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent e){
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED ){
try{
jep.setPage(e.getURL());
}catch(IOException et){
et.printStackTrace();
}
}
}
});//e.getURL()返回的不是aboutMe,而是有用的一个连接
jf.setContentPane(new JScrollPane(jep));
jf.setSize(new Dimension(300,300));
jf.setLocation(new Point(200,200));
jf.setVisible(true);
}
else if(e.getSource()==ji23){
JOptionPane.showMessageDialog(this, "我的记事本\n过去,豆鱼的天空");
}
}
class MySearch implements ActionListener{
JDialog searchDialog;
Container c;
JPanel faxian;
ButtonGroup jbg;
JRadioButton jr1;
JRadioButton jr2;
JLabel content;
final JButton jb;
JButton jbcan;
JCheckBox jc;
JTextField tf;
//String searchText;
public MySearch(){
position=0;
searchDialog=new JDialog(MyTxt.this,"查找",false);
faxian=new JPanel();
tf=new JTextField(15);
jbg=new ButtonGroup();
jr1=new JRadioButton("向上");
jr2=new JRadioButton("向下");
content=new JLabel("查找内容(N):");
jb=new JButton("查找下一个(F)");
jbcan=new JButton("取消");
jc=new JCheckBox("区分大小写");
c=searchDialog.getContentPane();
content.setDisplayedMnemonic('N');
jb.setMnemonic('F');
c.setLayout(null);
content.setBounds(5,10,80,25);
tf.setBounds(90,10,150,25);
jb.setBounds(255,10,115,25);
jbcan.setBounds(255,45,115,25);
jc.setBounds(5,55,100,25);
faxian.setBounds(110,38,130,55);
faxian.setBorder(BorderFactory.createTitledBorder("方向"));
jbg.add(jr1);
jbg.add(jr2);
jb.setEnabled(false);
tf.setText(savesearch);
tf.requestFocus();
tf.selectAll();
if(!tf.getText().equals(""))
jb.setEnabled(true);
tf.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent e){
if(!tf.getText().equals(""))
jb.setEnabled(true);
else
jb.setEnabled(false);
}
public void keyPressed(KeyEvent e){
if(!tf.getText().equals(""))
jb.setEnabled(true);
else
jb.setEnabled(false);
}
});
jr2.setSelected(true);
faxian.add(jr1);
faxian.add(jr2);
jb.addActionListener(this);
jbcan.addActionListener(this);
c.add(content);
c.add(tf);
c.add(jb);
c.add(faxian);
c.add(jc);
c.add(jbcan);
searchDialog.setLocation(200,200);
searchDialog.setSize(390,125);
searchDialog.setResizable(false);
searchDialog.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb){
savesearch=tf.getText();
endposition=savesearch.length();
if(jr1.isSelected())
selectjr=true;
else
selectjr=false;
if(jc.isSelected())
daxiao=true;
else
daxiao=false;
search();
}
else
searchDialog.dispose();
}
}
public void search(){//查找是从你的插入符位置开始查找
Txt.requestFocus();
String str=Txt.getText();
if(!selectjr){
if(daxiao){
position=str.indexOf(savesearch,Txt.getCaretPosition());
if(position==-1){
JOptionPane.showMessageDialog(null, "向下查找已经没有你所要查找的对象!!!");
return;
}
Txt.select(position, position+endposition);
}
else{
position=str.toLowerCase().indexOf(savesearch.toLowerCase(), Txt.getCaretPosition());
if(position==-1){
JOptionPane.showMessageDialog(null, "向下查找已经没有你所要查找的对象!!!");
return;
}
Txt.select(position, position+endposition);
}
}
else{
if(daxiao){
position=str.lastIndexOf(savesearch, Txt.getCaretPosition()-endposition-1);
if(position==-1){
JOptionPane.showMessageDialog(null, "向上查找已经没有你所要查找的对象!!!");
return;
}
Txt.select(position, position+endposition);
}
else{
position=str.toLowerCase().lastIndexOf(savesearch.toLowerCase(), Txt.getCaretPosition()-endposition-1);
if(position==-1){
JOptionPane.showMessageDialog(null, "向上查找已经没有你所要查找的对象!!!");
return;
}
Txt.select(position, position+endposition);
}
}
}
class Myreplace implements ActionListener{
JLabel cha,tihuan;
JTextField chafield,tifield;
JButton chabutton,tihuanbutton,allbutton,canbutton;
JCheckBox jcb;
JDialog replaceDialog=new JDialog(MyTxt.this,"替换",false);
public Myreplace(){
cha=new JLabel("查找内容(N):");
tihuan=new JLabel("替换为(P):");
jcb=new JCheckBox("区分大小写(C)");
chafield=new JTextField(15);
tifield=new JTextField(15);
chabutton=new JButton("查找下一个(F)");
tihuanbutton=new JButton("替换(R)");
allbutton=new JButton("全部替换(A)");
canbutton=new JButton("取消");
cha.setDisplayedMnemonic('N');
tihuan.setDisplayedMnemonic('P');
jcb.setMnemonic('C');
chabutton.setMnemonic('F');
tihuanbutton.setMnemonic('R');
allbutton.setMnemonic('A');
Container c=replaceDialog.getContentPane();
c.setLayout(null);
cha.setBounds(8,5,80,25);
chafield.setBounds(90,5,160,25);
tihuan.setBounds(8,35,80,25);
tifield.setBounds(90,35,160,25);
chabutton.setBounds(260,3,120,25);
tihuanbutton.setBounds(260,33,120,25);
allbutton.setBounds(260,63,120,25);
canbutton.setBounds(260,93,120,25);
jcb.setBounds(8,90,120,25);
chabutton.setEnabled(false);
tihuanbutton.setEnabled(false);
allbutton.setEnabled(false);
chafield.setText(savesearch);
chafield.selectAll();
chabutton.addActionListener(this);
tihuanbutton.addActionListener(this);
allbutton.addActionListener(this);
canbutton.addActionListener(this);
if(!chafield.getText().equals("")){
chabutton.setEnabled(true);
tihuanbutton.setEnabled(true);
allbutton.setEnabled(true);
}//为了实现当按纽可用或不可用,因为本人找不到有什么好的监听方法,所以用了这种...
chafield.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent e){
if(chafield.getText().equals("")){
chabutton.setEnabled(false);
tihuanbutton.setEnabled(false);
allbutton.setEnabled(false);
}
else{
chabutton.setEnabled(true);
tihuanbutton.setEnabled(true);
allbutton.setEnabled(true);
}
}
public void keyPressed(KeyEvent e){
if(chafield.getText().equals("")){
chabutton.setEnabled(false);
tihuanbutton.setEnabled(false);
allbutton.setEnabled(false);
}
else{
chabutton.setEnabled(true);
tihuanbutton.setEnabled(true);
allbutton.setEnabled(true);
}
}
});
c.add(cha);
c.add(tihuan);
c.add(jcb);
c.add(chabutton);
c.add(tihuanbutton);
c.add(allbutton);
c.add(canbutton);
c.add(chafield);
c.add(tifield);
replaceDialog.setResizable(false);
replaceDialog.setSize(390,170);
replaceDialog.setLocation(200, 200);
replaceDialog.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==chabutton){
savesearch=chafield.getText();
endposition=savesearch.length();
tihuandaxiao=jcb.isSelected();
//search();//这里的区分大小不同与查找类中的,可以用传参实现公用一个函数....
Txt.requestFocus();
String str=Txt.getText();
if(tihuandaxiao){
position=str.indexOf(savesearch,Txt.getCaretPosition());
if(position==-1){
JOptionPane.showMessageDialog(null, "向下查找已经没有你所要查找的对象!!!");
return;
}
Txt.select(position, position+endposition);
}
else{
position=str.toLowerCase().indexOf(savesearch.toLowerCase(), Txt.getCaretPosition());
if(position==-1){
JOptionPane.showMessageDialog(null, "向下查找已经没有你所要查找的对象!!!");
return;
}
Txt.select(position, position+endposition);
}
}
else if(e.getSource()==tihuanbutton){
Txt.requestFocus();
savesearch=chafield.getText();
tihuanText=tifield.getText();
endposition=savesearch.length();
tihuandaxiao=jcb.isSelected();
String str=Txt.getText();
if(tihuandaxiao){
if(Txt.getSelectedText()==null){
position=str.indexOf(savesearch,Txt.getCaretPosition());
if(position==-1){
JOptionPane.showMessageDialog(null, "向下查找已经没有你所要查找的对象!!!");
return;
}
Txt.select(position, position+endposition);
}
else{
Txt.replaceSelection(tihuanText);
str=Txt.getText();
position=str.indexOf(savesearch,Txt.getCaretPosition());
if(position==-1){
JOptionPane.showMessageDialog(null, "向下查找已经没有你所要查找的对象!!!");
return;
}
Txt.select(position, position+endposition);
}
}
else{
if(Txt.getSelectedText()==null){//这里不与""比较
position=str.toLowerCase().indexOf(savesearch.toLowerCase(), Txt.getCaretPosition());
if(position==-1){
JOptionPane.showMessageDialog(null, "向下查找已经没有你所要查找的对象!!!");
return;
}
Txt.select(position, position+endposition);
}
else{
Txt.replaceSelection(tihuanText);
str=Txt.getText();//因为文本内容已经改变,所以要重新赋值,这点让我苦想了很久.....
position=str.indexOf(savesearch,Txt.getCaretPosition());
if(position==-1){
JOptionPane.showMessageDialog(null, "向下查找已经没有你所要查找的对象!!!");
return;
}
Txt.select(position, position+endposition);
}
}
}
else if(e.getSource()==allbutton){
Txt.requestFocus();
savesearch=chafield.getText();
tihuanText=tifield.getText();
endposition=savesearch.length();
tihuandaxiao=jcb.isSelected();
String str=Txt.getText();
if(tihuandaxiao){
Txt.setText(str.replaceAll(savesearch, tihuanText));
}
else{
Txt.setText(str.replaceAll(savesearch.toLowerCase(), tihuanText));
str=Txt.getText();
Txt.setText(str.replaceAll(savesearch.toUpperCase(), tihuanText));
//这里可能有点问题,如果文本很长,会不会滚动两次滚动条.....
}
}
else
replaceDialog.dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -