Program 3.7.6:
~
Write a program to create three buttons “Square”, “Cube” and “Square root” to perform
operations accordingly. Keep the background color to “Yellow” and also change the backgrount
color to “Red” for clicked button.
import java.awt.*;
import java.awtevent.*;
class EventDemo6 extends Frame
{
Button btn] btn2,btn3;
TextField tf1,tf2;
public EventDemo6( )
{
setLayout(new FlowLayout( );
bin] = new Button("Square’)
btn2 = new Button("Cube");
bin3 = new Button("Sq Root");
fl = new TextField(15);
{2 = new TextField(15);
1f2.setEditable false);
add(tfl); add(btnl); add(btn2); add(btn3); add(2);
bin1.addFocusListener(new InnerFocus();
Scanned with CamScanner
yeeadvanced Java Programming (MSBTE) ao
Event Ha
inl addActionListener(new hinerSquare())s
pin2.addFocusListener(new InnerFocus( ));
bin2.add!
Listener(
pund.acddFos sw InnerFocus( ));
bin3.addActionListener(new InnerSqRoot( ));
fend of constr
{qass InnerFocus implements FocusListener
{
public void focusGained(FocusEvent fe)
{
Object ob = fe.zetSource();
iffob:
{
binl)
binl.setBackground(Color.ted);
bin2.setBackground(Color-yellow);
bin3.setBackground(Color:yellow);
bin2.setBackground(Color.red);
bin1.setBackground(Color.yellow)s
bin3.setBackground(Color.yellow);
}
iffob==bin3)
{
btn3,setBackground (Color.red)s
bin2,setBuckground(Color-yellow);
btn1 setBackground(Color.yellow)s
}
Public void focusLost(FocusEvent fe)
it
Scanned with CamScanner
¥
1%38 _Advanced Java Programming (MSBTE) at
} fiend of inner!
Jclass InnerSquate implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
int n = Integer.parselnt(tfL.getText(}}s
ints = nj
U2.setText(" + 5):
} fend of event()
} /end of inner2
lclass InnerCube implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
int n = Integer.parselnt(t{l getText( ))s
inte = nt
U2.setTest(" + os
} ifend of event()
} fend inner3
class InnerSqRoot implements ActionListener
H{
public void actionPerformed(ActionEyent ae)
{
int n = Integer.parselnt(tfl.getText()}s
double sq = Math.sqet(n)s
U2setText(" + 54);
} //end of event()
} i/end of inner
public static void main(String arf}
{
EventDemo6 fr = now EventDemo6( )s
fr-setSize(300,300)5
fr.setVisible(true)s
feserTitlo(’Demonstrating multiple events"):
ee
Scanned with CamScanner¥ Advanced Java Programming (MSBTE) 3-32 Event Handli
nd of class
Scanned with CamScanner