Java_Assignment_3_Solutions
Java_Assignment_3_Solutions
Java_Assignment_3_Solutions
import javax.swing.*;
frame.add(textArea);
frame.setSize(400, 300);
frame.setLayout(null);
frame.setVisible(true);
import javax.swing.*;
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.*;
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
import javax.swing.*;
import java.awt.event.*;
clicked!"));
clicked!"));
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 {
group.add(rb1);
group.add(rb2);
frame.add(rb1);
frame.add(rb2);
frame.add(label);
frame.setSize(400, 300);
frame.setLayout(null);
frame.setVisible(true);
import java.awt.event.*;
button.addActionListener(e -> {
int total = 0;
if (pizza.isSelected()) {
order.append("Pizza ");
total += 10;
}
if (burger.isSelected()) {
order.append("Burger ");
total += 5;
if (sandwich.isSelected()) {
order.append("Sandwich ");
total += 7;
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);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
t.start();
Interface.
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
t.start();
import java.sql.*;
try {
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username",
"password");
conn.close();
} catch (SQLException e) {
10. Write a program to enter the details of an employee in a window and add the record to the
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
button.addActionListener(e -> {
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username",
"password")) {
stmt.setString(1, name);
stmt.executeUpdate();
});
frame.add(nameLabel);
frame.add(nameField);
frame.add(button);
frame.add(result);
frame.setSize(400, 300);
frame.setLayout(null);
frame.setVisible(true);