class MyReplace extends JDialog
{
JLabel jl1,jl2,jl3;
JTextField jtf1,jtf2;
JCheckBox jcb;
MyPane mp;
JButton jb1,jb2,jb3,jb4;
JPanel jp1,jp2,jp3;
JPanel jp5,jp6,jp7,jp8;
public MyReplace()
{
this.setTitle("替換");
mp=new MyPane();
jp1=new JPanel();
jl1=new JLabel("查找的內容(N):");
jl2=new JLabel("替換為(P):");
jtf1=new JTextField(9);
jp5=new JPanel();
jp5.add(jtf1);
jtf2=new JTextField(9);
jp7=new JPanel();
jp7.add(jtf2);
jcb=new JCheckBox("區分大小寫(C)");
jb1=new JButton("查找下一個(F)");
jb1.setEnabled(false);
jb1.addActionListener(mp);
jp6=new JPanel();
jp6.add(jb1);
jp1.setLayout(new GridLayout(1,3));
jp1.add(jl1);
jp1.add(jp5);
jp1.add(jp6);
jb2=new JButton("替換(R)");
jb2.setEnabled(false);
jb2.addActionListener(mp);
jb3=new JButton("全部替換(A)");
jb3.setEnabled(false);
jb3.addActionListener(mp);
mp.setLayout(new GridLayout(2,1));
mp.add(jb2);
mp.add(jb3);
jb4=new JButton("取消");
jp8=new JPanel();
jp8.add(jb4);
jb4.addActionListener(mp);
jp2=new JPanel();
jp2.setLayout(new GridLayout(1,3));
jp2.add(jl2);
jp2.add(jp7);
jp2.add(mp);
jl3=new JLabel();
jp3=new JPanel();
jp3.setLayout(new GridLayout(1,3));
jp3.add(jcb);
jp3.add(jl3);
jp3.add(jp8);
this.setLayout(new GridLayout(3,3));
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setSize(400, 150);
this.setLocation(420, 280);
this.setVisible(true);
}
}
class MySearch extends JDialog
{
JLabel jl,jl1;
JTextField jtf;
JButton jb1,jb2;
JCheckBox jcb;
JRadioButton jrb1,jrb2;
ButtonGroup bg;
MyPanel mp;
JPanel jp1,jp2,jp3,jp4,jp5,jp6;
public MySearch()
{
this.setTitle("查找");
jl=new JLabel("查找內容(N):");
jtf=new JTextField(8);
jp6=new JPanel();
jp6.add(jtf);
jb1=new JButton("查找下一個(F):");
jb1.setEnabled(false);
jb2=new JButton("取消");
jcb=new JCheckBox("區分大小寫(C)");
jrb1=new JRadioButton("向上(U)");
jrb1.setSelected(true);
jrb2=new JRadioButton("向下(D)");
bg=new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
mp=new MyPanel();
mp.add(jb1);
jb1.addActionListener(mp);
jb2.addActionListener(mp);
jp1=new JPanel();
jp1.setBorder(BorderFactory.createTitledBorder("方向"));
jp1.setLayout(new GridLayout(1,2));
jp1.add(jrb1);
jp1.add(jrb2);
jp1.setPreferredSize(new Dimension(17,2));
jp2=new JPanel();
jp2.setLayout(new GridLayout(1,3));
jp2.add(jcb);
jp2.add(jp1);
jp3=new JPanel();
jp3.setLayout(new GridLayout(1,2));
jp3.add(jl);
jp3.add(jp6);
jp4=new JPanel();
jp4.setLayout(new GridLayout(1,2));
jp4.add(jp3);
jp4.add(mp);
jp5=new JPanel();
jp5.add(jb2);
jp2.add(jp5);
this.setLayout(new GridLayout(2,1));
this.add(jp4);
this.add(jp2);
j=pad.jta.getCaretPosition();
this.setSize(450, 150);
this.setLocation(420, 280);
this.setVisible(true);
}
}
class MyPane extends JPanel implements Runnable ,ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(mReplace.jb4==e.getSource())
{
mReplace.dispose();
}
if(e.getSource()==mReplace.jb3)
{
String str1=pad.jta.getText();
String str2=mReplace.jtf1.getText();
String str3=mReplace.jtf2.getText();
String str4=str1.toLowerCase();
String str5=str2.toLowerCase();
String str6=str3.toLowerCase();
String strA="",strB="",strC="";
if(mReplace.jcb.isSelected())
{
strA=str1;
strB=str2;
strC=str3;
}else
{
strA=str4;
strB=str5;
strC=str6;
}
pad.jta.setText(strA.replaceAll(strB,strC));
}
if(e.getSource()==mReplace.jb2)
{
String str1=pad.jta.getText();
String str2=mReplace.jtf1.getText();
String str3=mReplace.jtf2.getText();
String str4=str1.toLowerCase();
String str5=str2.toLowerCase();
String str6=str3.toLowerCase();
String strA="",strB="",strC="";
if(mReplace.jcb.isSelected())
{
strA=str1;
strB=str2;
strC=str3;
}else
{
strA=str4;
strB=str5;
strC=str6;
}
int s=strA.indexOf(strB,start);
if(strA.indexOf(strB,start)!=-1)
{
pad.jta.setText(strA.replaceFirst(strB,strC));
pad.jta.setSelectionStart(s);
pad.jta.setSelectionEnd(s+strB.length());
pad.jta.setSelectedTextColor(Color.blue);
start=s+1;
}else
{
JOptionPane.showMessageDialog(null, "查找完畢!", "提示", JOptionPane.WARNING_MESSAGE );
}
}
if(mReplace.jb1==e.getSource())
{
String str1=pad.jta.getText();
String str2=mReplace.jtf1.getText();
String str3=str1.toLowerCase();
String str4=str2.toLowerCase();
String strA="",strB="";
if(mReplace.jcb.isSelected())
{
strA=str1;
strB=str2;
}else
{
strA=str3;
strB=str4;
}
int s=strA.indexOf(strB,start);
if(strA.indexOf(strB,start)!=-1)
{
pad.jta.setSelectionStart(s);
pad.jta.setSelectionEnd(s+strB.length());
pad.jta.setSelectedTextColor(Color.blue);
start=s+1;
}else
{
JOptionPane.showMessageDialog(null, "查找完畢!", "提示", JOptionPane.WARNING_MESSAGE );
}
}
}
java基礎教程案例 (共54篇) 上一篇:java記事本代碼3 下一篇:java記事本5