Skip to content
Open

Done #21

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions part-1/E1.java
Original file line number Diff line number Diff line change
@@ -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");
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);

setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}

@Override
public void actionPerformed(ActionEvent e) {
count++;
counterLabel.setText("Clicks: " + count);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
E1 app = new E1();
app.setVisible(true);
});
}
}

54 changes: 54 additions & 0 deletions part-2/E2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class E2 extends JFrame implements ActionListener {


private JTextField nameField;
private JButton greetButton;
private JLabel messageLabel;


public E2() {
setTitle("Greeting Form");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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(350, 150);
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));
}
}
60 changes: 60 additions & 0 deletions part-3/E3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class E3 extends JFrame implements ActionListener {
private JPanel panel;
private JButton redButton;
private JButton greenButton;
private JButton blueButton;

public E3() {
setTitle("Theme Color Selector");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = 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(panel, BorderLayout.CENTER);

setSize(500, 400);
setLocationRelativeTo(null);
}

@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "red":
panel.setBackground(Color.RED);
break;
case "green":
panel.setBackground(Color.GREEN);
break;
case "blue":
panel.setBackground(Color.BLUE);
break;
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new E3().setVisible(true));
}
}
107 changes: 107 additions & 0 deletions part-4/E4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class E4 extends JFrame implements ActionListener {
private final JTextField txtName;
private final JTextField txtEmail;
private final JPasswordField txtPassword;
private final JRadioButton rbMale;
private final JRadioButton rbFemale;
private final JCheckBox cbJava;
private final JCheckBox cbPython;
private final JCheckBox cbCpp;

public E4() {
setTitle("User Registration");
setSize(400, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
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 genderGroup = new ButtonGroup();
genderGroup.add(rbMale);
genderGroup.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 = 1; gbc.gridy = 5;
gbc.anchor = GridBagConstraints.CENTER;
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(this);
panel.add(btnSubmit, gbc);

add(panel);
}

@Override
public void actionPerformed(ActionEvent e) {
String name = txtName.getText().trim();
String email = txtEmail.getText().trim();
String password = new String(txtPassword.getPassword());
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));
}
}