


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 4
This page cannot be seen from the preview
Don't miss anything!



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