Java Event Handling: Creating Buttons for Square, Cube, and Square Root Operations, Summaries of Mathematics

This java code demonstrates the creation of three buttons for performing square, cube, and square root operations. The background color changes for the clicked button while keeping the rest yellow. The text fields display the results.

Typology: Summaries

2020/2021

Uploaded on 12/09/2021

aditya-borle
aditya-borle 🇮🇳

1 document

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
/*WAP to creAte three buttons “squAre”, “cube”And “squAre root” to Perform
the oPerAtions Accordingly. keeP the bAckground color to “yelloW” And
Also chAnge the bAckground color to “red” for cliked button */
import java.awt.*;
import java.awt.event.*;
class EventDemo1 extends Frame
{
Button btn1,btn2,btn3;
TextField tf1,tf2;
public EventDemo1()
{
setLayout(new FlowLayout());
btn1=new Button("Square");
btn2=new Button("Cube");
btn3=new Button("Sq Root");
tf1=new TextField(15);
tf2=new TextField(15);
tf2.setEditable(false);
add(tf1);
add(btn1);
add(btn2);
add(btn3);
add(tf2);
btn1.addFocusListener(new InnerFocus());
btn1.addActionListener(new InnerSquare());
pf3
pf4

Partial preview of the text

Download Java Event Handling: Creating Buttons for Square, Cube, and Square Root Operations and more Summaries Mathematics in PDF only on Docsity!

/*WAP to creAte three buttons “squAre”, “cube”And “squAre root” to Perform

the oPerAtions Accordingly. keeP the bAckground color to “yelloW” And

Also chAnge the bAckground color to “red” for cliked button */

import java.awt.; import java.awt.event.; class EventDemo1 extends Frame { Button btn1,btn2,btn3; TextField tf1,tf2; public EventDemo1() { setLayout(new FlowLayout()); btn1=new Button("Square"); btn2=new Button("Cube"); btn3=new Button("Sq Root"); tf1=new TextField(15); tf2=new TextField(15); tf2.setEditable(false); add(tf1); add(btn1); add(btn2); add(btn3); add(tf2); btn1.addFocusListener(new InnerFocus()); btn1.addActionListener(new InnerSquare());

btn2.addFocusListener(new InnerFocus()); btn2.addActionListener(new InnerCube()); btn3.addFocusListener(new InnerFocus()); btn3.addActionListener(new InnerSqRoot()); }//end of constructor class InnerFocus implements FocusListener { public void focusGained(FocusEvent fe) { Object ob=fe.getSource(); if(ob==btn1) { btn1.setBackground(Color.red); btn2.setBackground(Color.yellow); btn3.setBackground(Color.yellow); } if(ob==btn2) { btn2.setBackground(Color.red); btn1.setBackground(Color.yellow); btn3.setBackground(Color.yellow); } if(ob==btn3) { btn3.setBackground(Color.red); btn2.setBackground(Color.yellow); btn1.setBackground(Color.yellow); } } public void focusLost(FocusEvent fe)

EventDemo1 fr=new EventDemo1(); fr.setSize(300,300); fr.setVisible(true); fr.setTitle("Demonstrating Multiple events"); } }//end of class