Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import javax.swing.border.Border;
- import java.awt.event.*;
- public class problem2 extends JFrame implements ActionListener {
- private Container c;
- private Font font;
- private JButton clr, val, regs;
- private JLabel reg, name, bday, gender, email, pw, repw, stts;
- private JRadioButton male, female, other;
- private ButtonGroup bg;
- private JTextField namef, emailf, pwf, repwf;
- private JComboBox<String> cb1, cb2, cb3;
- String day[] = new String[32];
- String year[] = new String[151];
- String month[] = {"month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
- problem2() {
- setSize( 1100, 550 );
- setLocationRelativeTo(null);
- setTitle("Registration");
- setResizable(true);
- init();
- }
- public void init() {
- // contentpane
- c = this.getContentPane();
- c.setLayout(null);
- font = new Font("Arial", Font.BOLD, 14);
- addJLabel();
- addTextBox();
- addButton();
- addComboBox();
- addRadioButton();
- }
- void addJLabel() {
- Border border = BorderFactory.createLineBorder(Color.black);
- // JLabel
- reg = new JLabel("Registration");
- name = new JLabel("Name");
- bday = new JLabel("Birthday");
- gender = new JLabel("Gender");
- email = new JLabel("Email");
- pw = new JLabel("Password");
- repw = new JLabel("Retype");
- stts = new JLabel();
- reg.setBounds(0, 0, 1100, 50);
- reg.setFont(font);
- reg.setHorizontalAlignment(JLabel.CENTER);
- reg.setBorder(border);
- c.add(reg);
- name.setBounds(50, 100, 150, 30);
- name.setFont(font);
- c.add(name);
- bday.setBounds(50, 150, 150, 30);
- bday.setFont(font);
- c.add(bday);
- gender.setBounds(50, 200, 150, 30);
- gender.setFont(font);
- c.add(gender);
- email.setBounds(50, 250, 150, 30);
- email.setFont(font);
- c.add(email);
- pw.setBounds(50, 300, 150, 30);
- pw.setFont(font);
- c.add(pw);
- repw.setBounds(500, 300, 150, 30);
- repw.setFont(font);
- c.add(repw);
- stts.setText("Status: ");
- stts.setBounds(50, 450, 1050, 30);
- stts.setFont(font);
- c.add(stts);
- }
- void addTextBox()
- {
- Border border = BorderFactory.createLineBorder(Color.black);
- namef = new JTextField();
- emailf = new JTextField();
- pwf = new JPasswordField();
- repwf = new JPasswordField();
- namef.setBounds(150, 100, 300, 30);
- namef.setBorder(border);
- namef.setFont(font);
- c.add(namef);
- emailf.setBounds(150, 250, 300, 30);
- emailf.setBorder(border);
- emailf.setFont(font);
- c.add(emailf);
- pwf.setBounds(150, 300, 300, 30);
- pwf.setBorder(border);
- pwf.setFont(font);
- c.add(pwf);
- repwf.setBounds(600, 300, 300, 30);
- repwf.setBorder(border);
- repwf.setFont(font);
- c.add(repwf);
- }
- void addButton()
- {
- Font font2 = new Font("Arial", Font.BOLD, 18);
- clr = new JButton("Clear");
- clr.setBounds(150, 350, 150, 50);
- clr.setFont(font2);
- clr.setOpaque(true);
- clr.setBackground(Color.BLUE);
- clr.setForeground(Color.WHITE);
- c.add(clr);
- val = new JButton("Validate");
- val.setBounds(400, 350, 150, 50);
- val.setFont(font2);
- val.setOpaque(true);
- val.setBackground(Color.BLUE);
- val.setForeground(Color.WHITE);
- c.add(val);
- regs = new JButton("Register");
- regs.setBounds(650, 350, 150, 50);
- regs.setFont(font2);
- regs.setOpaque(true);
- regs.setBackground(Color.BLUE);
- regs.setForeground(Color.WHITE);
- regs.setEnabled(false);
- c.add(regs);
- // actionlistener
- clr.addActionListener(this);
- val.addActionListener(this);
- regs.addActionListener(this);
- }
- void addComboBox()
- {
- day[0] = "day";
- for( int i = 1; i <= 31; i++ ) {
- String temp = ( Integer.toString(i) );
- day[i] = temp;
- }
- year[0] = "year";
- for( int i = 1, j = 1900; i < 151; i++, j++ ) {
- String temp = Integer.toString(j);
- year[i] = temp;
- }
- cb1 = new JComboBox<>( day );
- cb2 = new JComboBox<>( month );
- cb3 = new JComboBox<>( year );
- cb1.setBounds(150, 150, 100, 30);
- cb1.setFont(font);
- c.add(cb1);
- cb2.setBounds(300, 150, 100, 30);
- cb2.setFont(font);
- c.add(cb2);
- cb3.setBounds(450, 150, 100, 30);
- cb3.setFont(font);
- c.add(cb3);
- }
- void addRadioButton()
- {
- bg = new ButtonGroup();
- male = new JRadioButton("Male");
- female = new JRadioButton("Female");
- other = new JRadioButton("Other");
- male.setBounds(150, 200, 100, 30);
- male.setFont(font);
- c.add(male);
- female.setBounds(250, 200, 100, 30);
- female.setFont(font);
- c.add(female);
- other.setBounds(350, 200, 100, 30);
- other.setFont(font);
- c.add(other);
- bg.add(male);
- bg.add(female);
- bg.add(other);
- }
- public static void main(String[] args) throws Exception {
- problem2 frame = new problem2();
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if( e.getSource() == this.clr ) {
- this.namef.setText(null);
- this.emailf.setText(null);
- this.pwf.setText(null);
- this.repwf.setText(null);
- this.cb1.setModel(new DefaultComboBoxModel<String>(day));
- this.cb2.setModel(new DefaultComboBoxModel<String>(month));
- this.cb3.setModel(new DefaultComboBoxModel<String>(year));
- this.bg.clearSelection();
- stts.setText("Status: ");
- regs.setEnabled(false);
- }
- else if( e.getSource() == this.val ) {
- stts.setText("Status: Validation Successfull!");
- regs.setEnabled(true);
- String temp = new String();
- int i = 0;
- if( namef.getText().length() == 0 ) {
- i++;
- temp += "(" + Integer.toString(i) +") name ";
- }
- if( emailf.getText().length() == 0 ) {
- i++;
- temp += "(" + Integer.toString(i) +") email ";
- }
- if( pwf.getText().length() == 0 ) {
- i++;
- temp += "(" + Integer.toString(i) +") password ";
- }
- else if( !pwf.getText().toString().equals(repwf.getText().toString()) ) {
- System.out.println(pwf.getText().toString());
- System.out.println(repwf.getText().toString());
- i++;
- temp += "(" + Integer.toString(i) +") same password to both field ";
- }
- String temp2 = new String();
- if( i != 0 ) {
- temp = "Please enter: " + temp;
- stts.setText( temp );
- temp2 += " and select: ";
- regs.setEnabled(false);
- }
- else {
- temp2 += "Please select: ";
- regs.setEnabled(true);
- }
- i = 0;
- if( cb1.getSelectedItem().toString() == "day" ) {
- i++;
- temp2 += "(" + Integer.toString(i) +") birth date ";
- }
- if( cb2.getSelectedItem().toString() == "month" ) {
- i++;
- temp2 += "(" + Integer.toString(i) +") birth month ";
- }
- if( cb3.getSelectedItem().toString() == "year" ) {
- i++;
- temp2 += "(" + Integer.toString(i) +") birth year ";
- }
- if( bg.getSelection() == null ) {
- i++;
- temp2 += "(" + Integer.toString(i) +") Gender ";
- }
- if( i != 0 ) {
- stts.setText( "Status: " + temp + temp2 );
- regs.setEnabled(false);
- }
- }
- else if( e.getSource() == this.regs ) {
- stts.setText("Status: Registration Successful!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement