diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/JavaSwingSimpleExercise.iml b/.idea/JavaSwingSimpleExercise.iml new file mode 100644 index 0000000..1644d35 --- /dev/null +++ b/.idea/JavaSwingSimpleExercise.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f03c948 --- /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..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/JavaSwingSimpleExercise/E1.class b/out/production/JavaSwingSimpleExercise/E1.class new file mode 100644 index 0000000..b519346 Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/E1.class differ diff --git a/out/production/JavaSwingSimpleExercise/E2.class b/out/production/JavaSwingSimpleExercise/E2.class new file mode 100644 index 0000000..5125c30 Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/E2.class differ diff --git a/out/production/JavaSwingSimpleExercise/E3.class b/out/production/JavaSwingSimpleExercise/E3.class new file mode 100644 index 0000000..d6356ac Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/E3.class differ diff --git a/out/production/JavaSwingSimpleExercise/E4.class b/out/production/JavaSwingSimpleExercise/E4.class new file mode 100644 index 0000000..c3e91b8 Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/E4.class differ diff --git a/out/production/JavaSwingSimpleExercise/images/img-1-1.png b/out/production/JavaSwingSimpleExercise/images/img-1-1.png new file mode 100644 index 0000000..12b529a Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/images/img-1-1.png differ diff --git a/out/production/JavaSwingSimpleExercise/images/img-1-2.png b/out/production/JavaSwingSimpleExercise/images/img-1-2.png new file mode 100644 index 0000000..a78ac9e Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/images/img-1-2.png differ diff --git a/out/production/JavaSwingSimpleExercise/images/img-2-1.png b/out/production/JavaSwingSimpleExercise/images/img-2-1.png new file mode 100644 index 0000000..7025c4e Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/images/img-2-1.png differ diff --git a/out/production/JavaSwingSimpleExercise/images/img-2-2.png b/out/production/JavaSwingSimpleExercise/images/img-2-2.png new file mode 100644 index 0000000..f28a16e Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/images/img-2-2.png differ diff --git a/out/production/JavaSwingSimpleExercise/images/img-2-3.png b/out/production/JavaSwingSimpleExercise/images/img-2-3.png new file mode 100644 index 0000000..9ca5e8a Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/images/img-2-3.png differ diff --git a/out/production/JavaSwingSimpleExercise/images/img-3-1.png b/out/production/JavaSwingSimpleExercise/images/img-3-1.png new file mode 100644 index 0000000..489de5f Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/images/img-3-1.png differ diff --git a/out/production/JavaSwingSimpleExercise/images/img-3-2.png b/out/production/JavaSwingSimpleExercise/images/img-3-2.png new file mode 100644 index 0000000..c411ac3 Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/images/img-3-2.png differ diff --git a/out/production/JavaSwingSimpleExercise/images/img-3-3.png b/out/production/JavaSwingSimpleExercise/images/img-3-3.png new file mode 100644 index 0000000..fe6d687 Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/images/img-3-3.png differ diff --git a/out/production/JavaSwingSimpleExercise/images/img-3-4.png b/out/production/JavaSwingSimpleExercise/images/img-3-4.png new file mode 100644 index 0000000..969a178 Binary files /dev/null and b/out/production/JavaSwingSimpleExercise/images/img-3-4.png 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-1.md b/out/production/JavaSwingSimpleExercise/readme-part-1.md new file mode 100644 index 0000000..f7292df --- /dev/null +++ b/out/production/JavaSwingSimpleExercise/readme-part-1.md @@ -0,0 +1,67 @@ +## Exercise 1: Click Counter Application + +### Description & Goal + +Create a simple GUI application that tracks how many times a user clicks a button and displays the count in real time. This exercise reinforces the basics of Swing components and event handling. + +### Code Breakdown + +**Part 1: Field Declarations** +Define the UI components and a counter variable. + +```java +public class ClickCounterApp extends JFrame implements ActionListener { + private JLabel counterLabel; // Displays the click count + private JButton clickButton; // Button to register clicks + private int count = 0; // Stores the current count +} +``` + +*Explanation:* We declare a `JLabel` for showing the count, a `JButton` for clicks, and an integer `count` initialized to 0. + +**Part 2: Constructor Setup** +Initialize and arrange components in the window. + +```java +public ClickCounterApp() { + setTitle("Click Counter"); + counterLabel = new JLabel("Clicks: 0", SwingConstants.CENTER); + clickButton = new JButton("Click Me"); + clickButton.addActionListener(this); + + setLayout(new BorderLayout()); + add(counterLabel, BorderLayout.CENTER); + add(clickButton, BorderLayout.SOUTH); +} +``` + +*Explanation:* In the constructor we set the title, create the label and button, attach an `ActionListener` to the button, choose a `BorderLayout`, and place the label at center and button at bottom. + +**Part 3: Event Handling** +Update count and label text when the button is clicked. + +```java +@Override +public void actionPerformed(ActionEvent e) { + count++; // Increment the click counter + counterLabel.setText("Clicks: " + count); +} +``` + +*Explanation:* The `actionPerformed` method is called on each click. We increase `count` by one and refresh the label text to reflect the new value. + +**Part 4: Application Entry Point** +Launch the GUI on the Event Dispatch Thread. + +```java +public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new ClickCounterApp().setVisible(true)); +} +``` + +*Explanation:* Using `SwingUtilities.invokeLater` ensures thread safety by creating and showing the GUI on the Swing event thread. +--- +### output of the code: + +... +... diff --git a/out/production/JavaSwingSimpleExercise/readme-part-2.md b/out/production/JavaSwingSimpleExercise/readme-part-2.md new file mode 100644 index 0000000..4aaea9a --- /dev/null +++ b/out/production/JavaSwingSimpleExercise/readme-part-2.md @@ -0,0 +1,78 @@ +## Exercise 2: Greeting Form Application + +### Description & Goal + +Create a GUI form where the user enters their name into a text field and clicks a button to display a personalized greeting. This exercise practices input handling, text fields, and dynamic label updates. + +### Code Breakdown + +**Part 1: Field Declarations** +Define the input, button, and label components. + +```java +public class GreetingFormApp extends JFrame implements ActionListener { + private JTextField nameField; // Field for user input + private JButton greetButton; // Button to trigger greeting + private JLabel messageLabel; // Label to display greeting +} +``` + +*Explanation:* We declare a `JTextField` for entering the name, a `JButton` for submitting, and a `JLabel` for showing messages. + +**Part 2: Constructor Setup** +Initialize components and arrange them using a simple layout. + +```java +public GreetingFormApp() { + setTitle("Greeting Form"); + nameField = new JTextField(15); + greetButton = new JButton("Greet"); + messageLabel = new JLabel("Enter your name and press Greet.", SwingConstants.CENTER); + + greetButton.addActionListener(this); + + JPanel inputPanel = new JPanel(); + inputPanel.add(new JLabel("Name:")); + inputPanel.add(nameField); + inputPanel.add(greetButton); + + setLayout(new BorderLayout()); + add(inputPanel, BorderLayout.NORTH); + add(messageLabel, BorderLayout.CENTER); +} +``` + +*Explanation:* In the constructor, we set the title, instantiate the text field, button, and label, attach the listener, group the input components in a `JPanel`, and use `BorderLayout` to place the input panel at the top and the message label in the center. + +**Part 3: Event Handling** +Read the text field and update the label when the button is clicked. + +```java +@Override +public void actionPerformed(ActionEvent e) { + String name = nameField.getText().trim(); + if (name.isEmpty()) { + messageLabel.setText("Please enter a name."); + } else { + messageLabel.setText("Hello, " + name + "!"); + } +} +``` + +*Explanation:* On button click, we get the trimmed input. If empty, prompt the user; otherwise, display "Hello, {name}!". + +**Part 4: Application Entry Point** +Start the form on the Event Dispatch Thread. + +```java +public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new GreetingFormApp().setVisible(true)); +} +``` + +*Explanation:* We use `invokeLater` to ensure the GUI is created and manipulated on the Event Dispatch Thread, then display the frame. +--- +### output of the code: +... +... +... diff --git a/out/production/JavaSwingSimpleExercise/readme-part-3.md b/out/production/JavaSwingSimpleExercise/readme-part-3.md new file mode 100644 index 0000000..5491dba --- /dev/null +++ b/out/production/JavaSwingSimpleExercise/readme-part-3.md @@ -0,0 +1,89 @@ +## Exercise 3: Theme Color Selector Application + +### Description & Goal + +Create a UI with several color buttons ("Red", "Green", "Blue", ...) that, when clicked, change the background color of the main panel. This exercise reinforces component integration, action commands, and background styling. + +### Code Breakdown + +**Part 1: Field Declarations** +Define the main panel and color buttons. + +```java +public class ThemeColorSelectorApp extends JFrame implements ActionListener { + private JPanel mainPanel; // The panel whose background will change + private JButton redButton; // Button to select red theme + private JButton greenButton; // Button to select green theme + private JButton blueButton; // Button to select blue theme +} +``` + +*Explanation:* We declare a `JPanel` to hold the background, and three `JButton` instances for the color options. + +**Part 2: Constructor Setup** +Initialize components, set action commands, and arrange using `BorderLayout`. + +```java +public ThemeColorSelectorApp() { + setTitle("Theme Color Selector"); + + mainPanel = new JPanel(); + + redButton = new JButton("Red"); + greenButton = new JButton("Green"); + blueButton = new JButton("Blue"); + + redButton.setActionCommand("RED"); + greenButton.setActionCommand("GREEN"); + blueButton.setActionCommand("BLUE"); + + redButton.addActionListener(this); + greenButton.addActionListener(this); + blueButton.addActionListener(this); + + JPanel buttonPanel = new JPanel(); + buttonPanel.add(redButton); + buttonPanel.add(greenButton); + buttonPanel.add(blueButton); + + setLayout(new BorderLayout()); + add(buttonPanel, BorderLayout.NORTH); + add(mainPanel, BorderLayout.CENTER); +} +``` + +*Explanation:* In the constructor, we instantiate the panel and buttons, assign action commands to differentiate them in the listener, attach the same `ActionListener`, and place the buttons at the top with the main panel filling the center. + +**Part 3: Event Handling** +Change the `mainPanel` background based on the button clicked. + +```java +@Override +public void actionPerformed(ActionEvent e) { + switch (e.getActionCommand()) { + case "RED": mainPanel.setBackground(Color.RED); break; + case "GREEN": mainPanel.setBackground(Color.GREEN); break; + case "BLUE": mainPanel.setBackground(Color.BLUE); break; + } +} +``` + +*Explanation:* We check the action command of the event and apply the corresponding `Color` to the panel's background. + +**Part 4: Application Entry Point** +Launch the application safely on the Swing thread. + +```java +public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new ThemeColorSelectorApp().setVisible(true)); +} +``` + +*Explanation:* We use `invokeLater` to ensure the GUI is created and manipulated on the Event Dispatch Thread, then display the frame. +--- +### output of the code: +... +... +... +... + diff --git a/out/production/JavaSwingSimpleExercise/readme-part-4.md b/out/production/JavaSwingSimpleExercise/readme-part-4.md new file mode 100644 index 0000000..15777fb --- /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 E4().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/E1.java b/part-1/E1.java index e69de29..b60e7c4 100644 --- a/part-1/E1.java +++ b/part-1/E1.java @@ -0,0 +1,39 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class E1 extends JFrame implements ActionListener { + private JLabel counterLabel; + private JButton clickButton; + private int count = 0; + + public E1() { + setTitle("Click Counter"); + Font font = new Font("Segoe UI", Font.BOLD, 15); + + counterLabel = new JLabel("Clicks: 0", SwingConstants.CENTER); + counterLabel.setFont(font); + + clickButton = new JButton("Click Me"); + clickButton.setFont(font); + clickButton.addActionListener(this); + + setLayout(new BorderLayout()); + add(counterLabel, BorderLayout.CENTER); + add(clickButton, BorderLayout.SOUTH); + + setSize(400, 150); + setLocationRelativeTo(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + + @Override + public void actionPerformed(ActionEvent e) { + count++; + counterLabel.setText("Clicks: " + count); + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new E1().setVisible(true)); + } +} diff --git a/part-2/E2.java b/part-2/E2.java index e69de29..12b6cf8 100644 --- a/part-2/E2.java +++ b/part-2/E2.java @@ -0,0 +1,45 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class E2 extends JFrame implements ActionListener { + private JTextField nameField; + private JButton greetButton; + private JLabel messageLabel; + + public E2() { + setTitle("Greeting Form"); + nameField = new JTextField(15); + greetButton = new JButton("Greet"); + messageLabel = new JLabel("Enter your name and press Greet.", SwingConstants.CENTER); + + greetButton.addActionListener(this); + + JPanel inputPanel = new JPanel(); + inputPanel.add(new JLabel("Name:")); + inputPanel.add(nameField); + inputPanel.add(greetButton); + + setLayout(new BorderLayout()); + add(inputPanel, BorderLayout.NORTH); + add(messageLabel, BorderLayout.CENTER); + + setSize(400, 150); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + } + + @Override + public void actionPerformed(ActionEvent e) { + String name = nameField.getText().trim(); + if (name.isEmpty()) { + messageLabel.setText("Please enter a name."); + } else { + messageLabel.setText("Hello, " + name + "!"); + } + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new E2().setVisible(true)); + } +} diff --git a/part-3/E3.java b/part-3/E3.java index e69de29..410505f 100644 --- a/part-3/E3.java +++ b/part-3/E3.java @@ -0,0 +1,61 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class E3 extends JFrame implements ActionListener { + private JPanel colorPanel; + private JButton redButton; + private JButton greenButton; + private JButton blueButton; + + public E3() { + setTitle("Theme Color Selector"); + + redButton = new JButton("Red"); + greenButton = new JButton("Green"); + blueButton = new JButton("Blue"); + + redButton.setActionCommand("RED"); + greenButton.setActionCommand("GREEN"); + blueButton.setActionCommand("BLUE"); + + redButton.addActionListener(this); + greenButton.addActionListener(this); + blueButton.addActionListener(this); + + JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); + buttonPanel.add(redButton); + buttonPanel.add(greenButton); + buttonPanel.add(blueButton); + + colorPanel = new JPanel(); + colorPanel.setBackground(Color.WHITE); + + setLayout(new BorderLayout()); + add(buttonPanel, BorderLayout.NORTH); + add(colorPanel, BorderLayout.CENTER); + + setSize(400, 200); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + } + + @Override + public void actionPerformed(ActionEvent e) { + switch (e.getActionCommand()) { + case "RED": + colorPanel.setBackground(Color.RED); + break; + case "GREEN": + colorPanel.setBackground(Color.GREEN); + break; + case "BLUE": + colorPanel.setBackground(Color.BLUE); + break; + } + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new E3().setVisible(true)); + } +} diff --git a/part-4/E4.java b/part-4/E4.java index e69de29..223bd7c 100644 --- a/part-4/E4.java +++ b/part-4/E4.java @@ -0,0 +1,97 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class E4 extends JFrame implements ActionListener { + private JTextField txtName; + private JTextField txtEmail; + private JPasswordField txtPassword; + private JRadioButton rbMale, rbFemale; + private JCheckBox cbJava, cbPython, cbCpp; + private JButton btnSubmit; + private GridBagConstraints gbc; + + public E4() { + 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; + + gbc.gridx = 0; gbc.gridy = 0; + panel.add(new JLabel("Name:"), gbc); + gbc.gridx = 1; + txtName = new JTextField(20); + panel.add(txtName, gbc); + + gbc.gridx = 0; gbc.gridy = 1; + panel.add(new JLabel("Email:"), gbc); + gbc.gridx = 1; + txtEmail = new JTextField(20); + panel.add(txtEmail, gbc); + + gbc.gridx = 0; gbc.gridy = 2; + panel.add(new JLabel("Password:"), gbc); + gbc.gridx = 1; + txtPassword = new JPasswordField(20); + panel.add(txtPassword, 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(new FlowLayout(FlowLayout.LEFT)); + 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(new FlowLayout(FlowLayout.LEFT)); + interestPanel.add(cbJava); + interestPanel.add(cbPython); + interestPanel.add(cbCpp); + panel.add(interestPanel, gbc); + + gbc.gridx = 1; gbc.gridy = 5; + gbc.anchor = GridBagConstraints.CENTER; + btnSubmit = new JButton("Submit"); + btnSubmit.addActionListener(this); + panel.add(btnSubmit, gbc); + + add(panel); + } + + @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); + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new E4().setVisible(true)); + } +} diff --git a/part-4/readme-part-4.md b/part-4/readme-part-4.md index 2b65f53..15777fb 100644 --- a/part-4/readme-part-4.md +++ b/part-4/readme-part-4.md @@ -120,7 +120,7 @@ Run the form on the Swing event thread. ```java public static void main(String[] args) { - SwingUtilities.invokeLater(() -> new RegistrationFormApp().setVisible(true)); + SwingUtilities.invokeLater(() -> new E4().setVisible(true)); } ```