Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Button;
- import java.awt.Dimension;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public class Registration extends JFrame implements ActionListener {
- JFrame f;
- JPanel p;
- JTextField t1;
- JButton b1;
- public Registration() {
- f = new JFrame();
- f.setPreferredSize(new Dimension(400, 400)); // size of JFrame
- p = new JPanel();
- f.add(p);
- t1 = new JTextField(8);
- p.add(t1);
- b1 = new JButton("check"); // creating a JButton
- b1.addActionListener(this); // adding action listener
- p.add(b1); // adding JButton to panel
- f.pack();
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setVisible(true);
- }
- public static void main(String[] args) {
- new Registration();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == b1) {
- if (t1.getText().length() == 10 && t1.getText().matches("[0-9]+")) { // validate
- // the
- // string
- JOptionPane.showMessageDialog(f, " valid string");
- } else {
- JOptionPane.showMessageDialog(f, " Invalid string");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment