0% found this document useful (0 votes)
17 views

Java_Assignment_3_Solutions

Uploaded by

maardanga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java_Assignment_3_Solutions

Uploaded by

maardanga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Assignment Solutions - Assignment 3

1. Write a program to print text into Text Area using JTextArea.

import javax.swing.*;

public class JTextAreaExample {

public static void main(String[] args) {

JFrame frame = new JFrame("JTextArea Example");

JTextArea textArea = new JTextArea("Hello, JTextArea!");

textArea.setBounds(20, 20, 300, 200);

frame.add(textArea);

frame.setSize(400, 300);

frame.setLayout(null);

frame.setVisible(true);

2. Create this frame using JFrame.

import javax.swing.*;

public class JFrameExample {

public static void main(String[] args) {

JFrame frame = new JFrame("My JFrame Example");

JLabel label = new JLabel("This is a JFrame.");


label.setBounds(50, 50, 200, 30);

frame.add(label);

frame.setSize(400, 300);

frame.setLayout(null);

frame.setVisible(true);

3. Write a program to display selected item from JComboBox in a label on click of Show

button.

import javax.swing.*;

import java.awt.event.*;

public class JComboBoxExample {

public static void main(String[] args) {

JFrame frame = new JFrame("JComboBox Example");

String[] items = {"Item 1", "Item 2", "Item 3"};

JComboBox<String> comboBox = new JComboBox<>(items);

comboBox.setBounds(50, 50, 150, 30);

JLabel label = new JLabel("Selected: ");

label.setBounds(50, 150, 200, 30);

JButton button = new JButton("Show");

button.setBounds(50, 100, 80, 30);

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

label.setText("Selected: " +

comboBox.getItemAt(comboBox.getSelectedIndex()));

});

frame.add(comboBox);

frame.add(label);

frame.add(button);

frame.setSize(400, 300);

frame.setLayout(null);

frame.setVisible(true);

4. Create a swing application to display a menu bar where each menu item should display a

dialog box to show its working.

import javax.swing.*;

import java.awt.event.*;

public class MenuBarExample {

public static void main(String[] args) {

JFrame frame = new JFrame("MenuBar Example");

JMenuBar menuBar = new JMenuBar();

JMenu menu = new JMenu("File");


JMenuItem open = new JMenuItem("Open");

JMenuItem save = new JMenuItem("Save");

JMenuItem exit = new JMenuItem("Exit");

open.addActionListener(e -> JOptionPane.showMessageDialog(frame, "Open

clicked!"));

save.addActionListener(e -> JOptionPane.showMessageDialog(frame, "Save

clicked!"));

exit.addActionListener(e -> System.exit(0));

menu.add(open);

menu.add(save);

menu.add(exit);

menuBar.add(menu);

frame.setJMenuBar(menuBar);

frame.setSize(400, 300);

frame.setLayout(null);

frame.setVisible(true);

5. Create this frame message that will show according to the radio button clicked.

import javax.swing.*;

import java.awt.event.*;
public class RadioButtonExample {

public static void main(String[] args) {

JFrame frame = new JFrame("RadioButton Example");

JRadioButton rb1 = new JRadioButton("Option 1");

rb1.setBounds(50, 50, 100, 30);

JRadioButton rb2 = new JRadioButton("Option 2");

rb2.setBounds(50, 100, 100, 30);

ButtonGroup group = new ButtonGroup();

group.add(rb1);

group.add(rb2);

JLabel label = new JLabel();

label.setBounds(50, 150, 200, 30);

rb1.addActionListener(e -> label.setText("Option 1 Selected"));

rb2.addActionListener(e -> label.setText("Option 2 Selected"));

frame.add(rb1);

frame.add(rb2);

frame.add(label);

frame.setSize(400, 300);

frame.setLayout(null);

frame.setVisible(true);

6. Create this food ordering system.


import javax.swing.*;

import java.awt.event.*;

public class FoodOrderingSystem {

public static void main(String[] args) {

JFrame frame = new JFrame("Food Ordering System");

JLabel label = new JLabel("Select your food:");

label.setBounds(50, 50, 200, 30);

JCheckBox pizza = new JCheckBox("Pizza - $10");

pizza.setBounds(50, 100, 200, 30);

JCheckBox burger = new JCheckBox("Burger - $5");

burger.setBounds(50, 150, 200, 30);

JCheckBox sandwich = new JCheckBox("Sandwich - $7");

sandwich.setBounds(50, 200, 200, 30);

JButton button = new JButton("Order");

button.setBounds(50, 250, 80, 30);

JLabel result = new JLabel("");

result.setBounds(50, 300, 300, 30);

button.addActionListener(e -> {

int total = 0;

StringBuilder order = new StringBuilder("You ordered: ");

if (pizza.isSelected()) {

order.append("Pizza ");

total += 10;

}
if (burger.isSelected()) {

order.append("Burger ");

total += 5;

if (sandwich.isSelected()) {

order.append("Sandwich ");

total += 7;

order.append("- Total: $").append(total);

result.setText(order.toString());

});

frame.add(label);

frame.add(pizza);

frame.add(burger);

frame.add(sandwich);

frame.add(button);

frame.add(result);

frame.setSize(400, 400);

frame.setLayout(null);

frame.setVisible(true);

7. Write a program to implement the concept of threading by extending Thread Class.


class MyThread extends Thread {

public void run() {

for (int i = 1; i <= 5; i++) {

System.out.println("Thread running: " + i);

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

public class ThreadExample {

public static void main(String[] args) {

MyThread t = new MyThread();

t.start();

8. Write a program to implement the concept of threading by implementing Runnable

Interface.

class MyRunnable implements Runnable {

public void run() {

for (int i = 1; i <= 5; i++) {


System.out.println("Runnable running: " + i);

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

public class RunnableExample {

public static void main(String[] args) {

Thread t = new Thread(new MyRunnable());

t.start();

9. Write a Java program that connects to a database using JDBC.

import java.sql.*;

public class DatabaseConnection {

public static void main(String[] args) {

try {

Connection conn =

DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username",
"password");

System.out.println("Connected to the database successfully!");

conn.close();

} catch (SQLException e) {

System.out.println("An error occurred: " + e.getMessage());

10. Write a program to enter the details of an employee in a window and add the record to the

database on button click.

import javax.swing.*;

import java.awt.event.*;

import java.sql.*;

public class EmployeeDetails {

public static void main(String[] args) {

JFrame frame = new JFrame("Employee Details");

JLabel nameLabel = new JLabel("Name:");

nameLabel.setBounds(50, 50, 100, 30);

JTextField nameField = new JTextField();

nameField.setBounds(150, 50, 150, 30);

JButton button = new JButton("Add");

button.setBounds(150, 100, 80, 30);

JLabel result = new JLabel("");


result.setBounds(50, 150, 300, 30);

button.addActionListener(e -> {

String name = nameField.getText();

try (Connection conn =

DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username",

"password")) {

String query = "INSERT INTO employees (name) VALUES (?)";

PreparedStatement stmt = conn.prepareStatement(query);

stmt.setString(1, name);

stmt.executeUpdate();

result.setText("Employee added successfully!");

} catch (SQLException ex) {

result.setText("Error: " + ex.getMessage());

});

frame.add(nameLabel);

frame.add(nameField);

frame.add(button);

frame.add(result);

frame.setSize(400, 300);

frame.setLayout(null);

frame.setVisible(true);

You might also like