diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/JavaSwingSimpleExercise.iml b/.idea/JavaSwingSimpleExercise.iml new file mode 100644 index 0000000..ab9bca8 --- /dev/null +++ b/.idea/JavaSwingSimpleExercise.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..2d16929 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/expretions.txt b/expretions.txt new file mode 100644 index 0000000..adc399b --- /dev/null +++ b/expretions.txt @@ -0,0 +1 @@ +starting to work with swing diff --git a/out/production/JavaSwingSimpleExercise/E4.class b/out/production/JavaSwingSimpleExercise/E4.class new file mode 100644 index 0000000..e19f65b Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/E4.class differ diff --git a/out/production/JavaSwingSimpleExercise/RegistrationApp.class b/out/production/JavaSwingSimpleExercise/RegistrationApp.class new file mode 100644 index 0000000..e6a68fb Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/RegistrationApp.class differ diff --git a/out/production/JavaSwingSimpleExercise/images/img-4-1.png b/out/production/JavaSwingSimpleExercise/images/img-4-1.png new file mode 100644 index 0000000..137d23f Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/images/img-4-1.png differ diff --git a/out/production/JavaSwingSimpleExercise/images/img-4-2.png b/out/production/JavaSwingSimpleExercise/images/img-4-2.png new file mode 100644 index 0000000..994d9db Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/images/img-4-2.png differ diff --git a/out/production/JavaSwingSimpleExercise/images/img-4-3.png b/out/production/JavaSwingSimpleExercise/images/img-4-3.png new file mode 100644 index 0000000..7cdaa57 Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/images/img-4-3.png differ diff --git a/out/production/JavaSwingSimpleExercise/readme-part-4.md b/out/production/JavaSwingSimpleExercise/readme-part-4.md new file mode 100644 index 0000000..2b65f53 --- /dev/null +++ b/out/production/JavaSwingSimpleExercise/readme-part-4.md @@ -0,0 +1,132 @@ +## Exercise 4: User Registration Form Application + +### Description & Goal + +Design a comprehensive registration form using `GridBagLayout`. Students will arrange multiple Swing components—text fields, password field, radio buttons, checkboxes, and a submit button—in a responsive grid. The goal is to practice advanced layout management and component integration. + +### Code Breakdown + +**Part 1: Field Declarations** +Define all UI components and layout constraints. + +```java +public class RegistrationFormApp extends JFrame implements ActionListener { + private JTextField txtName; // User name input + private JTextField txtEmail; // User email input + private JPasswordField txtPassword; // Password input + private JRadioButton rbMale, rbFemale; // Gender selection + private JCheckBox cbJava, cbPython, cbCpp; // Interests selection + private JButton btnSubmit; // Submit form button + private GridBagConstraints gbc; // Layout constraints +} +``` + +*Explanation:* We declare fields for name, email, and password inputs; radio buttons for gender; checkboxes for interests; a submit button; and `GridBagConstraints` for positioning. + +**Part 2: Constructor Setup** +Initialize components, configure `GridBagLayout`, and add items. + +```java +public RegistrationFormApp() { + setTitle("User Registration"); + setSize(500, 350); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setLocationRelativeTo(null); + + JPanel panel = new JPanel(new GridBagLayout()); + gbc = new GridBagConstraints(); + gbc.insets = new Insets(5, 5, 5, 5); + gbc.fill = GridBagConstraints.HORIZONTAL; + + // Row 0: Name label & field + gbc.gridx = 0; gbc.gridy = 0; + panel.add(new JLabel("Name:"), gbc); + gbc.gridx = 1; + txtName = new JTextField(20); + panel.add(txtName, gbc); + + // Row 1: Email label & field + gbc.gridx = 0; gbc.gridy = 1; + panel.add(new JLabel("Email:"), gbc); + gbc.gridx = 1; + txtEmail = new JTextField(20); + panel.add(txtEmail, gbc); + + // Row 2: Password label & field + gbc.gridx = 0; gbc.gridy = 2; + panel.add(new JLabel("Password:"), gbc); + gbc.gridx = 1; + txtPassword = new JPasswordField(20); + panel.add(txtPassword, gbc); + + // Row 3: Gender selection + gbc.gridx = 0; gbc.gridy = 3; + panel.add(new JLabel("Gender:"), gbc); + gbc.gridx = 1; + rbMale = new JRadioButton("Male"); rbFemale = new JRadioButton("Female"); + ButtonGroup bg = new ButtonGroup(); bg.add(rbMale); bg.add(rbFemale); + JPanel genderPanel = new JPanel(); genderPanel.add(rbMale); genderPanel.add(rbFemale); + panel.add(genderPanel, gbc); + + // Row 4: Interests selection + gbc.gridx = 0; gbc.gridy = 4; + panel.add(new JLabel("Interests:"), gbc); + gbc.gridx = 1; + cbJava = new JCheckBox("Java"); cbPython = new JCheckBox("Python"); cbCpp = new JCheckBox("C++"); + JPanel interestPanel = new JPanel(); + interestPanel.add(cbJava); interestPanel.add(cbPython); interestPanel.add(cbCpp); + panel.add(interestPanel, gbc); + + // Row 5: Submit button + gbc.gridx = 1; gbc.gridy = 5; + gbc.anchor = GridBagConstraints.CENTER; + btnSubmit = new JButton("Submit"); + btnSubmit.addActionListener(this); + panel.add(btnSubmit, gbc); + + add(panel); +} +``` + +*Explanation:* We configure `GridBagLayout` with padding, add labels and inputs sequentially by grid coordinates, and group related radio and checkbox components in sub-panels. + +**Part 3: Event Handling** +Collect input values on submit and display them. + +```java +@Override +public void actionPerformed(ActionEvent e) { + String name = txtName.getText(); + String email = txtEmail.getText(); + String gender = rbMale.isSelected() ? "Male" : rbFemale.isSelected() ? "Female" : "Unspecified"; + String interests = ""; + if (cbJava.isSelected()) interests += "Java "; + if (cbPython.isSelected()) interests += "Python "; + if (cbCpp.isSelected()) interests += "C++ "; + + JOptionPane.showMessageDialog(this, + "Name: " + name + "\n" + + "Email: " + email + "\n" + + "Gender: " + gender + "\n" + + "Interests: " + interests, + "Registration Details", JOptionPane.INFORMATION_MESSAGE); +} +``` + +*Explanation:* On button press, we read each field's value, determine selected options, and show a summary dialog. + +**Part 4: Application Entry Point** +Run the form on the Swing event thread. + +```java +public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new RegistrationFormApp().setVisible(true)); +} +``` + +*Explanation:* Ensures safe creation and display of the GUI on the Event Dispatch Thread. +--- +### output of the code: +... +... +... diff --git a/part-1/ClickApp.java b/part-1/ClickApp.java new file mode 100644 index 0000000..1d76dd5 --- /dev/null +++ b/part-1/ClickApp.java @@ -0,0 +1,29 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class ClickApp extends JFrame implements ActionListener { + private JLabel clickLabel; + private JButton clickButton; + private int counter = 0; + + public ClickApp() { + setTitle("Click App"); + clickButton = new JButton("click Me"); + clickLabel = new JLabel("click : 0", SwingConstants.CENTER); + clickButton.addActionListener(this); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setSize(400, 400); + + setLayout(new BorderLayout()); + add(clickButton, BorderLayout.SOUTH); + add(clickLabel, BorderLayout.CENTER); + } + + @Override + public void actionPerformed(ActionEvent event) { + counter++; + clickLabel.setText("click : " + counter); + } +} diff --git a/part-1/E1.java b/part-1/E1.java index e69de29..99d9d84 100644 --- a/part-1/E1.java +++ b/part-1/E1.java @@ -0,0 +1,8 @@ +import javax.swing.*; + +public class E1 { + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new ClickApp().setVisible(true)); + } +} \ No newline at end of file diff --git a/part-2/E2.java b/part-2/E2.java index e69de29..f0a86f5 100644 --- a/part-2/E2.java +++ b/part-2/E2.java @@ -0,0 +1,8 @@ +import javax.swing.*; + +public class E2 { + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new GreetingApp().setVisible(true)); + } +} \ No newline at end of file diff --git a/part-2/GreetingApp.java b/part-2/GreetingApp.java new file mode 100644 index 0000000..ad726e9 --- /dev/null +++ b/part-2/GreetingApp.java @@ -0,0 +1,45 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class GreetingApp extends JFrame implements ActionListener { + private JLabel appLabel; + private JButton greetingButton; + private TextField nameField; + private final JLabel sticker; + + public GreetingApp() { + setSize(400, 100); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setLocationRelativeTo(null); + setTitle("Greeting Form"); + sticker = new JLabel("NAME :", SwingConstants.LEFT); + nameField = new TextField(15); + appLabel = new JLabel("Enter your name and greet", SwingConstants.CENTER); + greetingButton = new JButton("Greet"); + + greetingButton.addActionListener(this); + + JPanel appPanel = new JPanel(); + + appPanel.add(sticker); + appPanel.add(nameField); + appPanel.add(greetingButton); + + setLayout(new BorderLayout()); + add(appPanel, BorderLayout.NORTH); + add(appLabel, BorderLayout.CENTER); + + } + + @Override + public void actionPerformed(ActionEvent event) { + String name = nameField.getText().trim(); + if (!name.isEmpty()) { + appLabel.setText("hello " + name); + }else { + appLabel.setText("hello " + name + " !"); + } + } +} diff --git a/part-3/ColorApp.java b/part-3/ColorApp.java new file mode 100644 index 0000000..df7abd1 --- /dev/null +++ b/part-3/ColorApp.java @@ -0,0 +1,52 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class ColorApp extends JFrame implements ActionListener { + private JButton green; + private JButton blue; + private JButton red; + private JPanel panel; + + public ColorApp() { + setLocationRelativeTo(null); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setTitle("THEME COLOR APP"); + setSize(400, 300); + + panel = new JPanel(); + + green = new JButton("GREEN"); + blue = new JButton("BLUE"); + red = new JButton("red"); + + green.setActionCommand("green"); + blue.setActionCommand("blue"); + red.setActionCommand("red"); + + green.addActionListener(this); + blue.addActionListener(this); + red.addActionListener(this); + + JPanel buttonPanel = new JPanel(); + buttonPanel.add(green); + buttonPanel.add(blue); + buttonPanel.add(red); + + setLayout(new BorderLayout()); + add(panel, BorderLayout.CENTER); + add(buttonPanel, BorderLayout.NORTH); + } + + @Override + public void actionPerformed(ActionEvent event) { + if (event.getActionCommand().equalsIgnoreCase("green")){ + panel.setBackground(Color.GREEN); + }else if (event.getActionCommand().equalsIgnoreCase("blue")) { + panel.setBackground(Color.BLUE); + } else if (event.getActionCommand().equalsIgnoreCase("red")) { + panel.setBackground(Color.RED); + } + } +} diff --git a/part-3/E3.java b/part-3/E3.java index e69de29..de81d45 100644 --- a/part-3/E3.java +++ b/part-3/E3.java @@ -0,0 +1,8 @@ +import javax.swing.*; + +public class E3 { + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new ColorApp().setVisible(true)); + } +} \ No newline at end of file diff --git a/part-4/E4.java b/part-4/E4.java index e69de29..89e942f 100644 --- a/part-4/E4.java +++ b/part-4/E4.java @@ -0,0 +1,8 @@ +import javax.swing.*; + +public class E4 { + + public static void main(String[] args) { + SwingUtilities.invokeLater(() ->new RegistrationApp().setVisible(true)); + } +} \ No newline at end of file diff --git a/part-4/RegistrationApp.java b/part-4/RegistrationApp.java new file mode 100644 index 0000000..f2ed1ec --- /dev/null +++ b/part-4/RegistrationApp.java @@ -0,0 +1,100 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class RegistrationApp extends JFrame implements ActionListener { + private JLabel nameLabel; + private JLabel emailLabel; + private JLabel passwordLabel; + private JRadioButton rbMale, rbFemale; + private JCheckBox cbJava, cbPython, cbCpp; + private JTextField nameField; + private JTextField emailField; + private JPasswordField passwordField; + private GridBagConstraints gbc; + + + private JButton submit; + + public RegistrationApp() { + setTitle("USER REGISTRATION APP"); + setSize(500, 350); + setDefaultCloseOperation(EXIT_ON_CLOSE); + + JPanel panel = new JPanel(new GridBagLayout()); + gbc = new GridBagConstraints(); + gbc.insets = new Insets(5, 5, 5, 5); + gbc.fill = GridBagConstraints.HORIZONTAL; + + + nameLabel = new JLabel("NAME"); + emailLabel = new JLabel("E-MAIL"); + passwordLabel = new JLabel("PASSWORD"); + + gbc.gridx = 0; + gbc.gridy = 0; + panel.add(nameLabel = new JLabel("NAME : "), gbc); + gbc.gridx = 1; + panel.add(nameField = new JTextField(15), gbc); + + gbc.gridx = 0; + gbc.gridy = 1; + panel.add(emailLabel = new JLabel("E-MAIL : "), gbc); + gbc.gridx = 1; + panel.add(emailField = new JTextField(15), gbc); + + gbc.gridx = 0; + gbc.gridy = 2; + panel.add(passwordLabel = new JLabel("PASSWORD : "), gbc); + gbc.gridx = 1; + panel.add(passwordField = new JPasswordField(15), gbc); + + gbc.gridx = 0; gbc.gridy = 3; + panel.add(new JLabel("Gender:"), gbc); + gbc.gridx = 1; + rbMale = new JRadioButton("Male"); rbFemale = new JRadioButton("Female"); + ButtonGroup bg = new ButtonGroup(); bg.add(rbMale); bg.add(rbFemale); + JPanel genderPanel = new JPanel(); genderPanel.add(rbMale); genderPanel.add(rbFemale); + panel.add(genderPanel, gbc); + + gbc.gridx = 0; + gbc.gridy = 4; + panel.add(new JLabel("Interests:"), gbc); + gbc.gridx = 1; + cbJava = new JCheckBox("java");cbPython = new JCheckBox("python");cbCpp = new JCheckBox("C++"); + JPanel interestPanel = new JPanel(); + interestPanel.add(cbJava); interestPanel.add(cbPython); interestPanel.add(cbCpp); + panel.add(interestPanel, gbc); + + gbc.gridx = 0; + gbc.gridy = 5; + gbc.anchor = GridBagConstraints.CENTER; + submit = new JButton("SUBMIT"); + submit.addActionListener(this); + panel.add(submit, gbc); + + add(panel); + } + + @Override + public void actionPerformed(ActionEvent event) { + String name = nameField.getText(); + String email = emailField.getText(); + String gender = rbMale.isSelected() ? "male" : rbFemale.isSelected() ? "Female" : "unSpecified"; + String interest = ""; + interest += cbJava.isSelected() ? "JAVA " : ""; + interest += cbPython.isSelected()? "PYTHON " : ""; + interest += cbCpp.isSelected() ? "C++ " : ""; + + JOptionPane.showMessageDialog(this, + "Name: " + name + "\n" + + "Email: " + email + "\n" + + "Gender: " + gender + "\n" + + "Interests: " + interest, + "Registration Details", JOptionPane.INFORMATION_MESSAGE); + + } + + +}