2
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

public class Loginpanel extends JFrame {

    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField usr;
    private JPasswordField pass;
    private JButton submit;
    private JRadioButton admin;
    private JRadioButton student;
    private ButtonGroup type;

    public Loginpanel() 
    {
        super("User Login");
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setSize( 350, 200 ); // set frame size

        FieldHandler handler = new FieldHandler();


        setLayout(new GridBagLayout());
        setResizable(false);

        GridBagConstraints c = new GridBagConstraints();

        label3 = new JLabel();
        label3.setText("Login");
        c.weightx =0;
        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.VERTICAL;
        add(label3,c);

        label1 = new JLabel("Username:");
        c.weightx = 0;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        add(label1,c);

        usr = new JTextField(10);
        c.weightx = 0;
        c.gridx = 2;
        c.gridy = 2;
        c.ipadx = 2;

        add(usr,c);

        label2 = new JLabel("Password:");
        c.weightx = 0;
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        add(label2,c);

        pass = new JPasswordField(10);
        c.weightx = 0;
        c.gridx = 2;
        c.gridy = 3;
        c.ipadx= 2;
        add(pass,c);

        admin = new JRadioButton( "Admin", true );
        c.weightx = 0;
        c.gridx = 0;
        c.gridy = 4;
        add(admin,c);

        student = new JRadioButton( "Student", false );
        c.weightx = 0;
        c.gridx = 1;
        c.gridy = 4;
        add(student,c);

        submit = new JButton("Submit");
        c.weightx = 0;
        c.gridx = 1;
        c.gridy = 5;
        add(submit,c);

        type = new ButtonGroup();
        type.add(admin);
        type.add(student);

        usr.addActionListener( handler );
        pass.addActionListener( handler );
        submit.addActionListener(handler);

    }

    class FieldHandler implements ActionListener{

        public void actionPerformed(ActionEvent event)  {
            String string ="";
            if(event.getSource() == usr){
                string = String.format("%s",event.getActionCommand());

            }
            else if(event.getSource() == pass){
                string = String.format("%s",event.getActionCommand());

            }
            else if(event.getSource() == submit){
                string = "Submit Button";

            }
            JOptionPanel.showMessageDialog( null, string );
        }

    }

}

I am trying to make a Online examination system in Java, and i am new to the language. So i have made the neat form, with text fields, radio buttons, and a submit button. I checked out the event handler functions, and i am pretty much stuck right now. The event handler invokes the element which has an operation on it.Thats pretty much fine, but i cannot find a way to get the data on every element. event.getActionCommand() gets the information of just the element which invoked the handler. Another thing is I am not sure how to change the frame after the results from the database is fetched successfully, and if the password mismatches or no such user is found then it just shows a error message(I know it would be done by Joptionpanel) and returns.

The class which has the main function is

import java.awt.GridBagLayout;
import javax.swing.JFrame;

public class Start {

    public static void main(String args[]) {
        Loginpanel login = new Loginpanel();
        login.setVisible( true ); // display frame
    }

}
4

2 に答える 2