Java文本中的查找和替换
java吧
全部回复
仅看楼主
level 7
无我无梵 楼主
P1.Java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class P1 extends JFrame implements ActionListener {
JButton jb3;
JTextArea t=new JTextArea(15,25);
static String str;
P1(){
super("查找");
this.setDefaultCloseOperation(3);
jb3=new JButton("查找");
this.add(t);
this.add(jb3);
this.setLayout(new FlowLayout());
this.setVisible(true) ;
this.setSize(500,450) ;
this.setLocation(300,200) ;
this.setFocusable(true);
this.validate();
jb3.addActionListener(this);
}
public static void main(String[] args) {
new P1();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb3){
str=t.getText();
P2 ms=new P2();
ms.xun(str);
}
}
}
2014年06月19日 04点06分 1
level 7
无我无梵 楼主
P2.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class P2 extends JFrame implements ActionListener {
JButton jb1,jb2,jb3;
JLabel jl1,jl2;
JTextField t1,t2;
JPanel p;
String s;
P2(){
super("查找");
this.setDefaultCloseOperation(2);
init();
this.setLayout(new FlowLayout());
this.setVisible(true) ;
this.setSize(500,350) ;
this.setLocation(300,200) ;
this.setFocusable(true);
this.validate();
jb1.addActionListener(this);
jb2.addActionListener(this);
}
void init(){
p=new JPanel(new GridLayout(2,3,5,5));
jl1=new JLabel("查找内容(F):");
jl2=new JLabel("替换内容(F):");
jb1=new JButton("查找");
jb2=new JButton("替换");
t1=new JTextField(13);
t2=new JTextField(13);
p.add(jl1); p.add(t1);p.add(jb1);
p.add(jl2); p.add(t2);p.add(jb2);
this.add(p);
}
void xun(String s){
this.s=s;
}
void find(){ }
void replace(){ }
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1){ find(); }
if(e.getSource()==jb2){ replace(); }
}
}
2014年06月19日 04点06分 2
level 7
无我无梵 楼主
P2里面的find()和replace()这两个方法怎么写啊?实现查找和替换。
2014年06月19日 04点06分 3
主要是两个窗体间的交互
2014年06月20日 02点06分
level 11
String.indexOf&String.replace
2014年06月19日 05点06分 4
可是这个窗体间的交互应该怎么做啊?
2014年06月20日 01点06分
回复 无我无梵 :把p1写成p2的内部类
2014年06月20日 03点06分
level 14
2014年06月20日 02点06分 5
1