import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Regex extends JFrame { private final int fontsize = 24; private JTextField tRegex = new JTextField("[A-D]{1}[bce-h]{1,5}"); private JTextField tEingabe = new JTextField("Abc"); private JTextField tErgebnis = new JTextField(); public Regex () { setGUI(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } private void setGUI() { setLocation(200,200); setSize(600, 350); setTitle("Test mit regulären Ausdrücken"); this.getContentPane().setLayout( new GridBagLayout() ); // -------------------------------- JLabel label1 = new JLabel("Regulärer Ausdruck"); add(label1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(30,10,0,10), 0, 0)); add(tRegex, new GridBagConstraints(1, 0, 1, 1, 100.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(30,10,0,10), 0, 0)); label1.setFont( new Font("Arial", Font.BOLD,fontsize) ); tRegex.setFont( new Font("Arial", Font.PLAIN,fontsize) ); // ----------------------------------- JLabel label2 = new JLabel("Eingabetext"); add(label2, // 0, 0, 1, 1, 0.0, 0.0 Gridx, Gridy, Columns, Rows, Weightx, weighty new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30,10,0,10), 0, 0)); // 30,10,0,10 Ränder: Top, Left, Bottom, Right add(tEingabe, new GridBagConstraints(1, 1, 1, 1, 100.0, 0.0 ,GridBagConstraints.NORTHEAST, GridBagConstraints.HORIZONTAL, new Insets(30,10,0,10), 0, 0)); label2.setFont( new Font("Arial", Font.BOLD,fontsize) ); tEingabe.setFont( new Font("Arial", Font.PLAIN,fontsize) ); JLabel label3 = new JLabel("Ergebnis"); add(label3, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(60,10,0,10), 0, 0)); add(tErgebnis, new GridBagConstraints(1, 3, 1, 1, 100.0, 0.0 ,GridBagConstraints.NORTHEAST, GridBagConstraints.HORIZONTAL, new Insets(60,10,0,10), 0, 0)); label3.setFont( new Font("Arial", Font.BOLD,fontsize) ); tErgebnis.setFont( new Font("Arial", Font.PLAIN,fontsize) ); // Schalter JButton bnAction = new JButton("Check"); bnAction.setFont( new Font("Arial", Font.BOLD,fontsize+2) ); JPanel pn = new JPanel(); pn.setLayout( new FlowLayout() ); pn.add(bnAction); add(pn, new GridBagConstraints(0, 4, 2, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(30,0,0,0), 0, 0)); // Verbinden des Schalters mit der Methode "bnAction_Click" bnAction.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { bnAction_Click(); } }); // wenn in der Eingabezeile die return-Taste gedrückt wird tEingabe.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { bnAction_Click(); } }); tRegex.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { bnAction_Click(); } }); } // setGUI private void bnAction_Click() { try { String maske = tRegex.getText(); String text = tEingabe.getText(); // // if ( Pattern.matches( maske, text) ) if (text.matches(maske)) tErgebnis.setText("alles okay"); else tErgebnis.setText("Fehlerhafter Text"); } catch (Exception e ) { System.err.println(e.toString()); tErgebnis.setText(e.toString()); } } public static void main(String[] args) { new Regex(); } } // class