Create following Registration Form using Swing in Java.
Save entered data in array of strings. After Submit, following dialogue box should be displayed.
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);
}
}
Comments
Leave a comment