import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import static net.mindview.util.SwingConsole.*;
public class ex06 extends JFrame {
JTextArea text1 = new JTextArea(20,40);
JTextArea text2 = new JTextArea(20,40);
JTextField txt = new JTextField(20);
JButton bn = new JButton("button");
public ex06() {
bn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (text1.getText() == "") {
text2.append("Usage:\njava TestRegularExpression "
+ "characterSequence regularExpression+"+"\n");
return;
}
text2.append("Input: \"" + text1.getText() + "\""+"\n");
String arg = txt.getText();
text2.append("Regular expression: \"" + arg + "\""+"\n");
Pattern p = Pattern.compile(arg);
Matcher m = p.matcher(text1.getText());
while (m.find()) {
text2.append("Match \"" + m.group()
+ "\" at positions " + m.start() + "-"
+ (m.end() - 1)+"\n");
}
}
});
setLayout(new FlowLayout());
add(text1);
add(txt);
add(bn);
add(text2);
}
public static void main(String[]args){
run(new ex06(),800,400);
}
}
当JTextArea
是空时,getText()
返回的好像既不是null
也不是""
不知具体返回的是什么