package tool;
import java.awt.Cursor;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class RegularTest extends JFrame {
int id = 1;
private static Pattern pattern;
@SuppressWarnings("static-access")
public static boolean isRight(String input, String regex) {
pattern = Pattern.compile(regex);
return pattern.matches(regex, input);
}
private JButton jButton = new JButton("test");
private JTextArea jTextArea1 = new JTextArea(),
jTextArea2 = new JTextArea();
private JTextField jTextField1 = new JTextField("结果");
private LayoutManager layoutManager;
public RegularTest() {
layoutManager = new GridLayout(3, 2);
jTextArea1.setLineWrap(true);
jTextArea2.setLineWrap(true);
setLayout(layoutManager);
add(new JTextField("字符串"));
add(jTextArea1);
add(new JTextField("正则表达式"));
add(jTextArea2);
add(jTextField1);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField1.setText("结果:"
+ isRight(jTextArea1.getText(), jTextArea2.getText()));
if (id % 2 == 0) {
jTextField1.setText("结果");
jButton.setText("test");
} else {
jButton.setText("cancle");
}
id++;
}
});
Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
jButton.setCursor(cursor);
add(jButton);
}
// 入口
public static void main(String[] args) {
SwingHelp.run(new RegularTest(), 300, 200);
}
}
class SwingHelp {
public static void run(final JFrame jFrame, final int x, final int y) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jFrame.setTitle(jFrame.getClass().getSimpleName());
jFrame.setLocation(500, 350);
jFrame.setSize(x, y);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
图形没有整,就写了个功能。