Compile, run, and analyze the code and output of the testButton.java and handleT
ID: 3622542 • Letter: C
Question
Compile, run, and analyze the code and output of the testButton.java and handleThis.java programs. Write a report giving a description of each of the marked lines of code (1..5).import java.awt.*;
public class testButton
{
private Frame f;
private Button b;
public testButton()
{
f = new Frame("Javateer IQ Test");
b = new Button("Press This Button Now Javateer");
b.setActionCommand("The Pressing of the Button"); //(1)
}
public void launchFrame()
{
b.addActionListener(new handleThis()); //(2)
f.add(b, BorderLayout.CENTER);
f.pack();
f.setVisible(true); //(3)
}
public static void main(String Jim[])
{
testButton gapp = new testButton();
gapp.launchFrame();
}// end Main
}// end Class
import java.awt.event.*;
public class handleThis implements ActionListener
{
public void actionPerformed(ActionEvent jim) //(4)
{
System.out.println("The Action Did Occur");
System.out.println("The Button is: " +jim.getActionCommand()); //(5)
System.exit(0);
}
}
Explanation / Answer
please rate - thanks
1--this make it so that when something happens to button b "The Pressing of the Button" is written.
when something happens actionPerformed is started - it gets where the action came from (4) and this is put into jim. when the action came from the button being pushed it outputs "The button is" and goes to the action created in testButton for button b which is "The Pressing of the Button (1) and what is specified for that action being done (5)
2 makes it so that button B has an actionListener on it (something to watch for an event(action) to happen on the button b and when that happens it triggers actionPerformed to start executing.
3 makes it so that we can see the frame that was created
--hope this makes sense if not message me
import java.awt.*;
public class testButton
{
private Frame f;
private Button b;
public testButton()
{
f = new Frame("Javateer IQ Test");
b = new Button("Press This Button Now Javateer");
b.setActionCommand("The Pressing of the Button"); //(1)
}
public void launchFrame()
{
b.addActionListener(new handleThis()); //(2)
f.add(b, BorderLayout.CENTER);
f.pack();
f.setVisible(true); //(3)
}
public static void main(String Jim[])
{
testButton gapp = new testButton();
gapp.launchFrame();
}// end Main
}// end Class
import java.awt.event.*;
public class handleThis implements ActionListener
{
public void actionPerformed(ActionEvent jim) //(4)
{
System.out.println("The Action Did Occur");
System.out.println("The Button is: " +jim.getActionCommand()); //(5)
System.exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.