Page No:
1. Write a program to design a form components textbox, text field,checkbox, buttons, list and
handle various events related to each component.
// program1
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Pro1 {
public int i;
public Pro1(){
Frame fr = new Frame("Home Assignment Answer 1");
Label l1 = new Label("Name : ");
TextField textBox = new TextField();
Label l2 = new Label("Course : ");
List list = new List(7);
Label l3 = new Label("Address : ");
TextArea textArea = new TextArea();
Checkbox check = new Checkbox("I Agree That All The Above Details.",false);
Button button = new Button("Submit");
//Frame
fr.setSize(500,300);
fr.setLayout(null);
fr.setVisible(true);
//Label
l1.setBounds(35,50,50,30);
fr.add(l1);
l2.setBounds(35,100,50,30);
fr.add(l2);
l3.setBounds(260,50,60,30);
fr.add(l3);
//TextField
textBox.setBounds(90,55,150,20);
fr.add(textBox);
//List
list.add("Civil");
list.add("Architecture");
list.add("Computer Science And Technology");
list.add("Electrical");
list.add("Automobile");
list.add("Pharmacy");
list.add("Electrical Instrumentation");
list.setBounds(90,100,100,55);
fr.add(list);
//TextArea
textArea.setBounds(320,50,120,150);
fr.add(textArea);
//Checkbox
check.setBounds(35,150, 200,50);
fr.add(check);
//Button
button.setBounds(220,250,48,20);
fr.add(button);
fr.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0); //type-2 code To Close frame
}
});
}
public static void main(String[] args){
new Pro1();
}
}
OUTPUT:
Branch: Roll: No:
Registration No: Subject:
Full Signature:
Page No:
2. Write a program to design a calculator using Java components and handle various events
related to each component and apply proper layout to it.
package com.company;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Pro2 {
private JFrame frame;
private JTextField textField;
double first, second, result;
String operation, answer;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Pro2 window = new Pro2();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Pro2() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 426, 458);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.BOLD, 16));
textField.setBackground(Color.WHITE);
textField.setBounds(12, 13, 384, 66);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnBack = new JButton("\uF0E7");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String backSpace = null;
if (textField.getText().length() > 0) {
StringBuilder str = new StringBuilder(textField.getText());
str.deleteCharAt(textField.getText().length() - 1);
backSpace = str.toString();
textField.setText(backSpace);
}
}
});
btnBack.setBounds(12, 92, 86, 51);
btnBack.setFont(new Font("Wingdings", Font.BOLD, 15));
frame.getContentPane().add(btnBack);
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField.getText() + btn7.getText();
textField.setText(num);
}
});
btn7.setBounds(12, 156, 86, 51);
btn7.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btn7);
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField.getText() + btn4.getText();
textField.setText(num);
}
});
btn4.setBounds(12, 220, 86, 51);
btn4.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btn4);
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField.getText() + btn1.getText();
textField.setText(num);
}
});
btn1.setBounds(12, 284, 86, 51);
btn1.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btn1);
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField.getText() + btn0.getText();
textField.setText(num);
}
});
btn0.setBounds(12, 347, 86, 51);
btn0.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btn0);
JButton btnClear = new JButton("C");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(null);
}
});
btnClear.setBounds(111, 92, 86, 51);
btnClear.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btnClear);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField.getText() + btn8.getText();
textField.setText(num);
}
});
btn8.setBounds(111, 156, 86, 51);
btn8.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btn8);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField.getText() + btn5.getText();
textField.setText(num);
}
});
btn5.setBounds(111, 220, 86, 51);
btn5.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btn5);
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField.getText() + btn2.getText();
textField.setText(num);
}
});
btn2.setBounds(111, 284, 86, 51);
btn2.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btn2);
JButton btnDot = new JButton(".");
btnDot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField.getText() + btnDot.getText();
textField.setText(num);
}
});
btnDot.setBounds(111, 347, 86, 51);
btnDot.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btnDot);
JButton btn00 = new JButton("00");
btn00.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField.getText() + btn00.getText();
textField.setText(num);
}
});
btn00.setBounds(211, 92, 86, 51);
btn00.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btn00);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField.getText() + btn6.getText();
textField.setText(num);
}
});
btn6.setBounds(211, 220, 86, 51);
btn6.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btn6);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField.getText() + btn9.getText();
textField.setText(num);
}
});
btn9.setBounds(211, 156, 86, 51);
btn9.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btn9);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField.getText() + btn3.getText();
textField.setText(num);
}
});
btn3.setBounds(211, 284, 86, 51);
btn3.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btn3);
JButton btnEqual = new JButton("=");
btnEqual.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
second = Double.parseDouble(textField.getText());
if (operation == "+") {
result = first + second;
answer = String.format("%.2f", result);
textField.setText(answer);
} else if (operation == "-") {
result = first - second;
answer = String.format("%.2f", result);
textField.setText(answer);
} else if (operation == "X") {
result = first * second;
answer = String.format("%.2f", result);
textField.setText(answer);
} else if (operation == "/") {
result = first / second;
answer = String.format("%.2f", result);
textField.setText(answer);
} else if (operation == "%") {
result = first % second;
answer = String.format("%.2f", result);
textField.setText(answer);
}
}
});
btnEqual.setBounds(211, 347, 86, 51);
btnEqual.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btnEqual);
JButton btnMinus = new JButton("-");
btnMinus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = Double.parseDouble(textField.getText());
textField.setText("");
operation = "-";
}
});
btnMinus.setBounds(310, 156, 86, 51);
btnMinus.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btnMinus);
JButton btnMultiply = new JButton("X");
btnMultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = Double.parseDouble(textField.getText());
textField.setText("");
operation = "X";
}
});
btnMultiply.setBounds(310, 220, 86, 51);
btnMultiply.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btnMultiply);
JButton btnDivision = new JButton("/");
btnDivision.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = Double.parseDouble(textField.getText());
textField.setText("");
operation = "/";
}
});
btnDivision.setBounds(310, 284, 86, 51);
btnDivision.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btnDivision);
JButton btnPercent = new JButton("%");
btnPercent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = Double.parseDouble(textField.getText());
textField.setText("");
operation = "%";
}
});
btnPercent.setBounds(310, 347, 86, 51);
btnPercent.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
frame.getContentPane().add(btnPercent);
JButton btnPlus = new JButton("+");
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = Double.parseDouble(textField.getText());
textField.setText("");
operation = "+";
}
});
btnPlus.setFont(new Font("Bookman Old Style", Font.BOLD, 15));
btnPlus.setBounds(310, 91, 86, 51);
frame.getContentPane().add(btnPlus);
}
}
Branch: Roll: No:
Registration No: Subject:
Full Signature:
Page No:
Branch: Roll: No:
Registration No: Subject:
Full Signature:
Page No:
Branch: Roll: No:
Registration No: Subject:
Full Signature: