Answer to Question #296513 in Java | JSP | JSF for Ram

Question #296513

Create following Registration Form using Swing in Java.


Save entered data in array of strings. After Submit, following dialogue box should be displayed.



1
Expert's answer
2022-02-11T13:40:14-0500
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String[]> data = new ArrayList<>();
        JFrame frame = new JFrame("Registration");
        JPanel panel = new JPanel();
        BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
        panel.setLayout(boxLayout);
        JLabel loginJLabel = new JLabel("Login");
        loginJLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        loginJLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
        JTextField loginJTextField = new JTextField();
        loginJTextField.setAlignmentX(Component.CENTER_ALIGNMENT);
        loginJTextField.setAlignmentY(Component.CENTER_ALIGNMENT);
        loginJTextField.setMaximumSize(new Dimension(120, 20));
        JLabel passwordJLabel = new JLabel("Password");
        passwordJLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        passwordJLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
        JTextField passwordJTextField = new JTextField();
        passwordJTextField.setAlignmentX(Component.CENTER_ALIGNMENT);
        passwordJTextField.setAlignmentY(Component.CENTER_ALIGNMENT);
        passwordJTextField.setMaximumSize(new Dimension(120, 20));
        JButton submitJButton = new JButton("Submit");
        submitJButton.setSize(50, 20);
        submitJButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        submitJButton.setAlignmentY(Component.CENTER_ALIGNMENT);
        submitJButton.addActionListener(event -> {
            if (loginJTextField.getText().length() > 0 && passwordJTextField.getText().length() > 0) {
                data.add(new String[]{loginJTextField.getText(), passwordJLabel.getText()});
                loginJTextField.setText("");
                passwordJTextField.setText("");
                JOptionPane.showMessageDialog(frame, "Success", "Registration", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        panel.add(Box.createGlue());
        panel.add(loginJLabel);
        panel.add(Box.createRigidArea(new Dimension(0, 10)));
        panel.add(loginJTextField);
        panel.add(Box.createRigidArea(new Dimension(0, 10)));
        panel.add(passwordJLabel);
        panel.add(Box.createRigidArea(new Dimension(0, 10)));
        panel.add(passwordJTextField);
        panel.add(Box.createRigidArea(new Dimension(0, 10)));
        panel.add(submitJButton);
        panel.add(Box.createGlue());
        frame.add(panel);


        frame.setSize(350, 250);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS