4039 Event Handling
4039 Event Handling
1.Aim: Write a temperature conversion applet that converts from Fahrenheit to Celsius. The
Fahrenheit temperature should be entered from the keyboard (via a JTextField). A JLabel
should be used to display the converted temperature. Use the following formula for the
conversion: Celcius = ((5/9)*(Ferenheit‐32)). Enhance the temperature conversion applet of
Q1 by adding the Kelvin temperature scale. The applet should also allow the user to make
conversions between any two scales. Use the following formula for the conversion between
Kelvin and Celsius (in addition to the formula in Q1): Kelvin = Celcius + 273.15
Algorithm:
Program:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
/*
</applet>
*/
JTextField txtInput;
JLabel lblResult;
JRadioButton rbCelcius, rbKelvin;
conpane.add(txtInput);
conpane.add(rbCelcius);
conpane.add(rbKelvin);
selection.add(rbCelcius);
selection.add(rbKelvin);
button1.addActionListener(this);
conpane.add(button1);
lblResult= new JLabel ("Enter Ferenheit, Choose an option to convert and Click Show Result");
conpane.add(lblResult);
}
public void actionPerformed(ActionEvent e) {
if (rbKelvin.isSelected())
answer += 273.15;
lblResult.setText(String.valueOf(df.format(answer)));
Output :
Algorithm:
5.When the user presses the control, the event is generated and handled.
7.Stop
Program:
import java.awt.*;
import java.awt.event.*;
TextField name,pass;
Button b1,b2;
MyLoginWindow()
setLayout(new FlowLayout());
this.setLayout(null);
name=new TextField(20);
pass=new TextField(20);
pass.setEchoChar('#');
b1=new Button("submit");
b2=new Button("cancel");
this.add(n);
this.add(name);
this.add(p);
this.add(pass);
this.add(b1);
this.add(b2);
n.setBounds(70,90,90,60);
p.setBounds(70,130,90,60);
name.setBounds(200,100,90,20);
pass.setBounds(200,140,90,20);
b1.setBounds(100,260,70,40);
b2.setBounds(180,260,70,40);
ml.setVisible(true);
ml.setSize(400,400);
Output:
3.Aim: Design a calculator using event-driven programming paradigm of Java with the following
options.
a) Decimal manipulations
b) Scientific manipulations
Algorithm:
1.Import the swing packages and awt packages.
3.Create the container and add controls for digits, scientific calculations and decimal
Manipulations.
5.When the user presses the control, the event is generated and handled.
7.Stop
Program
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class ScientificCalculator extends JFrame implements ActionListener
JTextField tfield;
int k = 1, x = 0, y = 0, z = 0;
char ch;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, zero, clr, pow2, pow3, exp,
fac, plus, min, div, log, rec, mul, eq, addSub, dot, mr, mc, mp,
Container cont;
ScientificCalculator()
cont = getContentPane();
cont.setLayout(new BorderLayout());
tfield.setHorizontalAlignment(SwingConstants.RIGHT);
tfield.addKeyListener(new KeyAdapter()
char c = keyevent.getKeyChar();
else
{
keyevent.consume();
});
textpanel.add(tfield);
boolean t = true;
mr = new JButton("MR");
buttonpanel.add(mr);
mr.addActionListener(this);
mc = new JButton("MC");
buttonpanel.add(mc);
mc.addActionListener(this);
mp = new JButton("M+");
buttonpanel.add(mp);
mp.addActionListener(this);
mm = new JButton("M-");
buttonpanel.add(mm);
mm.addActionListener(this);
b1 = new JButton("1");
buttonpanel.add(b1);
b1.addActionListener(this);
b2 = new JButton("2");
buttonpanel.add(b2);
b2.addActionListener(this);
b3 = new JButton("3");
buttonpanel.add(b3);
b3.addActionListener(this);
b4 = new JButton("4");
buttonpanel.add(b4);
b4.addActionListener(this);
b5 = new JButton("5");
buttonpanel.add(b5);
b5.addActionListener(this);
b6 = new JButton("6");
buttonpanel.add(b6);
b6.addActionListener(this);
b7 = new JButton("7");
buttonpanel.add(b7);
b7.addActionListener(this);
b8 = new JButton("8");
buttonpanel.add(b8);
b8.addActionListener(this);
b9 = new JButton("9");
buttonpanel.add(b9);
b9.addActionListener(this);
buttonpanel.add(zero);
zero.addActionListener(this);
buttonpanel.add(plus);
plus.addActionListener(this);
min.addActionListener(this);
buttonpanel.add(mul);
mul.addActionListener(this);
div.addActionListener(this);
buttonpanel.add(div);
buttonpanel.add(addSub);
addSub.addActionListener(this);
buttonpanel.add(dot);
dot.addActionListener(this);
eq = new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);
buttonpanel.add(rec);
rec.addActionListener(this);
buttonpanel.add(sqrt);
sqrt.addActionListener(this);
buttonpanel.add(log);
log.addActionListener(this);
sin.addActionListener(this);
buttonpanel.add(cos);
cos.addActionListener(this);
buttonpanel.add(tan);
tan.addActionListener(this);
buttonpanel.add(pow2);
pow2.addActionListener(this);
buttonpanel.add(pow3);
pow3.addActionListener(this);
exp.addActionListener(this);
buttonpanel.add(exp);
fac.addActionListener(this);
buttonpanel.add(fac);
buttonpanel.add(clr);
clr.addActionListener(this);
cont.add("Center", buttonpanel);
cont.add("North", textpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
String s = e.getActionCommand();
if (s.equals("1"))
if (z == 0)
tfield.setText(tfield.getText() + "1");
else {
tfield.setText("");
tfield.setText(tfield.getText() + "1");
z = 0;
if (s.equals("2")) {
if (z == 0) {
tfield.setText(tfield.getText() + "2");
else
tfield.setText("");
tfield.setText(tfield.getText() + "2");
z = 0;
if (s.equals("3")) {
if (z == 0) {
tfield.setText(tfield.getText() + "3");
else
tfield.setText("");
tfield.setText(tfield.getText() + "3");
z = 0;
if (s.equals("4")) {
if (z == 0) {
tfield.setText(tfield.getText() + "4");
else
tfield.setText("");
tfield.setText(tfield.getText() + "4");
z = 0;
if (s.equals("5")) {
if (z == 0) {
tfield.setText(tfield.getText() + "5");
else
{
tfield.setText("");
tfield.setText(tfield.getText() + "5");
z = 0;
if (s.equals("6")) {
if (z == 0) {
tfield.setText(tfield.getText() + "6");
else
tfield.setText("");
tfield.setText(tfield.getText() + "6");
z = 0;
if (s.equals("7")) {
if (z == 0) {
tfield.setText(tfield.getText() + "7");
else
tfield.setText("");
tfield.setText(tfield.getText() + "7");
z = 0;
}
if (s.equals("8")) {
if (z == 0) {
tfield.setText(tfield.getText() + "8");
else
tfield.setText("");
tfield.setText(tfield.getText() + "8");
z = 0;
if (s.equals("9")) {
if (z == 0) {
tfield.setText(tfield.getText() + "9");
else
tfield.setText("");
tfield.setText(tfield.getText() + "9");
z = 0;
if (s.equals("0"))
if (z == 0) {
tfield.setText(tfield.getText() + "0");
}
else
tfield.setText("");
tfield.setText(tfield.getText() + "0");
z = 0;
if (s.equals("AC")) {
tfield.setText("");
x = 0;
y = 0;
z = 0;
if (s.equals("log"))
if (tfield.getText().equals("")) {
tfield.setText("");
else
a = Math.log(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
if (s.equals("1/x")) {
if (tfield.getText().equals("")) {
tfield.setText("");
else
a = 1 / Double.parseDouble(tfield.getText());
tfield.setText("");
tfield.setText(tfield.getText() + a);
if (s.equals("Exp")) {
if (tfield.getText().equals("")) {
tfield.setText("");
else
a = Math.exp(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
if (s.equals("x^2")) {
if (tfield.getText().equals("")) {
tfield.setText("");
else
a = Math.pow(Double.parseDouble(tfield.getText()), 2);
tfield.setText("");
tfield.setText(tfield.getText() + a);
if (s.equals("x^3")) {
if (tfield.getText().equals("")) {
tfield.setText("");
else
a = Math.pow(Double.parseDouble(tfield.getText()), 3);
tfield.setText("");
tfield.setText(tfield.getText() + a);
if (s.equals("+/-")) {
if (x == 0) {
tfield.setText("-" + tfield.getText());
x = 1;
else
tfield.setText(tfield.getText());
if (s.equals(".")) {
if (y == 0) {
tfield.setText(tfield.getText() + ".");
y = 1;
else
tfield.setText(tfield.getText());
if (s.equals("+"))
if (tfield.getText().equals(""))
tfield.setText("");
temp = 0;
ch = '+';
else
temp = Double.parseDouble(tfield.getText());
tfield.setText("");
ch = '+';
y = 0;
x = 0;
tfield.requestFocus();
if (s.equals("-"))
{
if (tfield.getText().equals(""))
tfield.setText("");
temp = 0;
ch = '-';
else
x = 0;
y = 0;
temp = Double.parseDouble(tfield.getText());
tfield.setText("");
ch = '-';
tfield.requestFocus();
if (s.equals("/")) {
if (tfield.getText().equals(""))
tfield.setText("");
temp = 1;
ch = '/';
else
x = 0;
y = 0;
temp = Double.parseDouble(tfield.getText());
ch = '/';
tfield.setText("");
tfield.requestFocus();
if (s.equals("*")) {
if (tfield.getText().equals(""))
tfield.setText("");
temp = 1;
ch = '*';
else
x = 0;
y = 0;
temp = Double.parseDouble(tfield.getText());
ch = '*';
tfield.setText("");
tfield.requestFocus();
if (s.equals("MC"))
m1 = 0;
tfield.setText("");
if (s.equals("MR"))
tfield.setText("");
tfield.setText(tfield.getText() + m1);
if (s.equals("M+"))
if (k == 1) {
m1 = Double.parseDouble(tfield.getText());
k++;
else
m1 += Double.parseDouble(tfield.getText());
tfield.setText("" + m1);
if (s.equals("M-"))
if (k == 1) {
m1 = Double.parseDouble(tfield.getText());
k++;
else
{
m1 -= Double.parseDouble(tfield.getText());
tfield.setText("" + m1);
if (s.equals("Sqrt"))
if (tfield.getText().equals(""))
tfield.setText("");
else
a = Math.sqrt(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
if (s.equals("SIN"))
if (tfield.getText().equals(""))
tfield.setText("");
else
a = Math.sin(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
if (s.equals("COS"))
if (tfield.getText().equals(""))
tfield.setText("");
else
a = Math.cos(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
if (s.equals("TAN")) {
if (tfield.getText().equals("")) {
tfield.setText("");
else
a = Math.tan(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
if (s.equals("="))
if (tfield.getText().equals(""))
tfield.setText("");
else
temp1 = Double.parseDouble(tfield.getText());
switch (ch)
case '+':
break;
case '-':
break;
case '/':
break;
case '*':
break;
tfield.setText("");
tfield.setText(tfield.getText() + result);
z = 1;
}
if (s.equals("n!"))
if (tfield.getText().equals(""))
tfield.setText("");
else
a = fact(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
tfield.requestFocus();
double fact(double x)
int er = 0;
if (x < 0)
er = 20;
return 0;
double i, s = 1;
return s;
try
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
catch (Exception e)
f.setTitle("ScientificCalculator");
f.pack();
f.setVisible(true);
Output