diff --git a/part-1/E1.java b/part-1/E1.java deleted file mode 100644 index e69de29..0000000 diff --git a/part-1/src/main/ClickCounterApp.java b/part-1/src/main/ClickCounterApp.java new file mode 100644 index 0000000..a64a0ba --- /dev/null +++ b/part-1/src/main/ClickCounterApp.java @@ -0,0 +1,33 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class ClickCounterApp extends JFrame implements ActionListener { + private int count = 0; // Stores the current count + private JLabel counterLabel; + private JButton clickButton; + + public ClickCounterApp() { + setTitle("Click Counter"); + // Displays the click count + counterLabel = new JLabel("Clicks: 0", SwingConstants.CENTER); + // Button to register clicks + clickButton = new JButton("Click Me"); + clickButton.addActionListener(this); + + setLayout(new BorderLayout()); + add(counterLabel, BorderLayout.CENTER); + add(clickButton, BorderLayout.SOUTH); + + } + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new ClickCounterApp().setVisible(true)); + } + + @Override + public void actionPerformed(ActionEvent e) { + count++; + counterLabel.setText("Clicks: " + count); + } +} diff --git a/part-2/E2.java b/part-2/E2.java deleted file mode 100644 index e69de29..0000000 diff --git a/part-2/readme-part-2.md b/part-2/readme-part-2.md index 4aaea9a..25f4e0c 100644 --- a/part-2/readme-part-2.md +++ b/part-2/readme-part-2.md @@ -10,7 +10,7 @@ Create a GUI form where the user enters their name into a text field and clicks Define the input, button, and label components. ```java -public class GreetingFormApp extends JFrame implements ActionListener { +public class main.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 @@ -23,7 +23,7 @@ public class GreetingFormApp extends JFrame implements ActionListener { Initialize components and arrange them using a simple layout. ```java -public GreetingFormApp() { +public main.GreetingFormApp() { setTitle("Greeting Form"); nameField = new JTextField(15); greetButton = new JButton("Greet"); @@ -65,6 +65,8 @@ public void actionPerformed(ActionEvent e) { Start the form on the Event Dispatch Thread. ```java +import main.GreetingFormApp; + public static void main(String[] args) { SwingUtilities.invokeLater(() -> new GreetingFormApp().setVisible(true)); } diff --git a/part-2/src/main/GreetingFormApp.java b/part-2/src/main/GreetingFormApp.java new file mode 100644 index 0000000..41ae643 --- /dev/null +++ b/part-2/src/main/GreetingFormApp.java @@ -0,0 +1,41 @@ +package main; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +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 + 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); + } + @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 GreetingFormApp().setVisible(true)); + } +} \ No newline at end of file diff --git a/part-3/E3.java b/part-3/E3.java deleted file mode 100644 index e69de29..0000000 diff --git a/part-3/out/production/part-3/main/ThemeColorSelectorApp.class b/part-3/out/production/part-3/main/ThemeColorSelectorApp.class new file mode 100644 index 0000000..a33ffbc Binary files /dev/null and b/part-3/out/production/part-3/main/ThemeColorSelectorApp.class differ diff --git a/part-3/src/main/ThemeColorSelectorApp.java b/part-3/src/main/ThemeColorSelectorApp.java new file mode 100644 index 0000000..d11741c --- /dev/null +++ b/part-3/src/main/ThemeColorSelectorApp.java @@ -0,0 +1,52 @@ +package main; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +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 + + 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); + } + + @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; + } + } + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new ThemeColorSelectorApp().setVisible(true)); + } +} \ No newline at end of file diff --git a/part-4/E4.java b/part-4/E4.java deleted file mode 100644 index e69de29..0000000 diff --git a/part-4/out/production/part-4/main/RegistrationFormApp.class b/part-4/out/production/part-4/main/RegistrationFormApp.class new file mode 100644 index 0000000..b160363 Binary files /dev/null and b/part-4/out/production/part-4/main/RegistrationFormApp.class differ diff --git a/part-4/src/main/RegistrationFormApp.java b/part-4/src/main/RegistrationFormApp.java new file mode 100644 index 0000000..00c4ff9 --- /dev/null +++ b/part-4/src/main/RegistrationFormApp.java @@ -0,0 +1,96 @@ +package main; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.*; + +public class RegistrationFormApp extends JFrame implements ActionListener{ + private JTextField txtName; + private JTextField txtEmail; + private JTextField txtPassword; + private JRadioButton rbMale,rbFemale,rbNo; + private JCheckBox cbJava,cbPython,cbCpp; + private JButton btnSubmit; + private GridBagConstraints gbc; + public RegistrationFormApp(){ + setTitle("Registration Form"); + 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 JTextField(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"); rbNo = new JRadioButton("Other"); + ButtonGroup bg = new ButtonGroup(); bg.add(rbMale); bg.add(rbFemale); + bg.add(rbNo); + JPanel genderPanel = new JPanel(); genderPanel.add(rbMale); + genderPanel.add(rbFemale); genderPanel.add(rbNo); + 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 = 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 RegistrationFormApp().setVisible(true)); + } +} \ No newline at end of file